比方说,我有一个按钮和一个EditText,它们在同一高度(屏幕底部)对齐并共享屏幕宽度。我希望按钮只占用它所需的空间,editText用于覆盖其余的宽度。在相对布局中划分元素之间的屏幕宽度?
到目前为止,我一直在使用linearLayout,在这种情况下,我可以将这两个元素放在一个水平线性布局中,将editText的权重设置为1,将按钮的权重设置为wrap_content,会工作。
但是,我无法使用相对布局复制此行为。我可以将editText放在父级的底部,然后将按钮设置到其右侧,但我不知道如何使editText填充按钮空间的其余宽度。
什么是正确的做法呢?
编辑:这是我到目前为止有:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:hint="@string/editText_Hint"
android:inputType="number" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@id/edittext"
android:onClick="sendAnswer"
android:text="@string/button_text" />
</RelativeLayout>
而且我想复制这种行为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
/*other stuff here*/
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/editText_Hint"
android:inputType="number"
android:layout_weight="1"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:onClick="sendAnswer"/>
</LinearLayout>
</LinearLayout>
请把你的XML代码 – 2014-11-01 12:39:34
@Dilavar编辑的代码现在。 – kace91 2014-11-01 12:44:14
首先按钮(以XML格式)宽度包装内容,父母的底部和右侧(对齐)。下一步编辑左下角父母(对齐)和按钮宽度0dp – Selvin 2014-11-01 12:44:47