ab1*_*b11 84 android android-layout
这似乎很简单,但我无法弄清楚如何做到这一点.我有一个带有EditText和两个ImageButtons的水平布局.我希望ImageButtons具有固定大小,并且EditText占用布局中的剩余空间.如何实现这一目标?
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
<ImageButton
android:src="@drawable/img1"
android:layout_width="50dip"
android:layout_height="50dip">
</ImageButton>
<ImageButton
android:src="@drawable/img2"
android:layout_width="50dip"
android:layout_height="50dip">
</ImageButton>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Jon*_*han 134
如果要保持布局相同(即文本后面的按钮),请使用layout_weight属性,以及fill_parent:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
<ImageButton
android:src="@drawable/img1"
android:layout_width="50dip"
android:layout_height="50dip">
</ImageButton>
<ImageButton
android:src="@drawable/img2"
android:layout_width="50dip"
android:layout_height="50dip">
</ImageButton>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
赋予EditText'权重'使得它最后得出结论并优先于按钮.玩不同的layout_weights,看看你有多大的乐趣!
Sun*_*dey 49
在相对布局中尝试这个
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/btn1"
android:src="@drawable/icon"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentRight="true"/>
<ImageButton
android:id="@+id/btn2"
android:src="@drawable/icon"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_toLeftOf="@+id/btn1"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/btn2">
</EditText>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
Jos*_*arl 19
好的:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/button1"
android:src="@drawable/img1"
android:layout_width="50dip"
android:layout_alignParentTop="true"
android:layout_height="50dip">
</ImageButton>
<ImageButton
android:src="@drawable/img2"
android:layout_width="50dip"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/button1"
android:layout_height="50dip">
</ImageButton>
<EditText
android:layout_below="@id/button"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
</EditText>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
布局的关键点是:
fill_parent文件中的后期仅考虑剩余空间,而不是所有可能占用的空间.没有别的东西在那里fill_parent(match_parent2.2+),因此它占用了剩余的可用空间抱歉没看过你的问题就够了.如果您想以垂直方式执行此操作,上面的代码将提供答案.对于水平布局,@ Sunun发布的代码应该有效.
| 归档时间: |
|
| 查看次数: |
72043 次 |
| 最近记录: |