我有一个垂直布局:2 个文本字段和一个水平按钮栏:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff00ff00"
android:orientation="vertical"
android:padding="4dp" >
<EditText
android:id="@+id/et_email"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:background="#ff00ffff"
android:inputType="textEmailAddress" >
</EditText>
<EditText
android:id="@+id/et_comment"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:background="#ff00ffff"
android:inputType="textMultiLine"
android:maxLines="5"
android:minLines="5" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_send"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id="@+id/btn_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some very long text" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
我希望按钮栏具有 wrap_content
大小策略(甚至不能让它们固定大小,因为这个应用程序有不同的本地化)。我还希望文本字段与按钮框具有相同的宽度。以下是上面的 xml 在选择平台 17 的情况下在设计器 (Eclipse + ADT) 中的外观(以及我希望它的外观)。颜色只是为了方便调试:
这是它在 Android 4.1 平板电脑上的外观:
此布局用作对话框的自定义布局。除了设置内容和标题之外,没有对对话框进行任何操作。
有趣的事实:它在 Android 3.1 平板电脑上看起来完全符合我的预期(并且与 ADT 设计器显示的相同)。但 4.1 上不行。
如何实现所需的布局?
最佳答案
您可能想使用RelativeLayout 来完成您正在寻找的内容。尝试将您的 xml 更改为以下内容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff00ff00"
android:padding="4dp" >
<EditText
android:id="@+id/et_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonbar"
android:layout_alignRight="@+id/buttonbar"
android:layout_marginTop="2dp"
android:background="#ff00ffff"
android:inputType="textEmailAddress" >
</EditText>
<EditText
android:id="@+id/et_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonbar"
android:layout_alignRight="@+id/buttonbar"
android:layout_below="@+id/et_email"
android:layout_marginTop="2dp"
android:background="#ff00ffff"
android:inputType="textMultiLine"
android:maxLines="5"
android:minLines="5" />
<RelativeLayout
android:id="@+id/buttonbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et_comment" >
<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btn_show"
android:layout_alignTop="@+id/btn_show"
android:layout_toLeftOf="@+id/btn_show"
android:text="OK" />
<Button
android:id="@+id/btn_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Some very long text" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btn_show"
android:layout_alignTop="@+id/btn_show"
android:layout_toRightOf="@+id/btn_show"
android:text="Cancel" />
</RelativeLayout>
</RelativeLayout>
关于android - 让一个元素填充基于wrap_content的布局中的所有可用空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17594925/