【发布时间】:2013-07-12 17:07:56
【问题描述】:
我有一个垂直布局: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 大小策略(甚至不能让它们固定大小,因为这个应用程序有不同的本地化)。我还希望文本字段与按钮框具有相同的宽度。以下是选择平台 17 的设计器(Eclipse + ADT)中上述 xml 的外观(以及我希望它的外观)。颜色只是为了方便调试:
这是它在 Android 4.1 平板电脑上的外观:
此布局用作对话框的自定义布局。除了设置内容和标题之外,没有对 Dialog 进行任何操作。
有趣的事实:它在 Android 3.1 平板电脑上看起来与我想要的完全一样(并且与 ADT 设计师展示的一样)。但不是在 4.1 上。
我怎样才能达到想要的布局?
【问题讨论】:
-
我不明白你的问题(或者它很简单,只需在你的中间按钮上放一个 layout_weight=1 )
-
很确定他希望线性布局宽度缩小到 3 个按钮的宽度(就像在顶部图片中一样)
-
@njzk2:按钮有
wrap_content。对话框本身和两个文本字段应与带有按钮的布局一样宽。正如您在最后一张图片中看到的那样,情况并非如此。 -
一目了然,一切都在您的 xml 中。我会尝试一些建议,如果要使用 wrap_content 作为高度,请从 EditTexts 中删除权重,否则将高度设置为 0dp,它将使用权重。另外,出于好奇,如果您将按钮移到顶部,EditTexts 上方,它的大小是否合适?
-
您尝试过我发布的解决方案吗?据我所知,它完全符合您的要求。
标签: android android-layout user-interface