比方說,我有一個按鈕和一個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