2014-11-01 40 views
1

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

請把你的XML代碼 – 2014-11-01 12:39:34

+0

@Dilavar編輯的代碼現在。 – kace91 2014-11-01 12:44:14

+0

首先按鈕(以XML格式)寬度包裝內容,父母的底部和右側(對齊)。下一步編輯左下角父母(對齊)和按鈕寬度0dp – Selvin 2014-11-01 12:44:47

回答

1

試試這個,對齊按鈕和設置按鈕右側edittextmatch_parentwidth適合我。

<RelativeLayout 
    android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/button1" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 
</RelativeLayout> 

這樣做edittext看起來好像走到了邊界。爲了有更好的外觀效果的屬性添加一定的餘量給edittext右側android:layout_marginRight="5dp"

UPDATE

如果你的要求是使用父佈局爲Relative Layout,供您使用嵌套的最佳解決方案佈局。

使用嵌套在相對佈局中的LinearLayout,其對齊android:layout_alignParentBottom="true"與LinearLayout對齊。

樣品如下,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" > 

     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" > 

      <requestFocus /> 
     </EditText> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 

    </LinearLayout> 

</RelativeLayout> 
+0

這是迄今爲止我所得到的最佳解決方案,但我仍然不喜歡這種方式,正如您所說,editText似乎正在填充與按鈕重疊的寬度。我可以通過試驗和錯誤手動更改佈局:邊距,直到它匹配按鈕的寬度,但必須有更好的方法(當按鈕的寬度在不同的語言中更改時,加硬編碼的數字可能不起作用) – kace91 2014-11-01 12:59:18

+1

@ kace91看看我的更新。如果您可以使用嵌套在相對佈局中的線性佈局,那將會很有幫助。這個實現不使用硬編碼的值 – codePG 2014-11-01 13:20:59

+1

是的,我知道這是有效的,但我試圖避免嵌套佈局,因爲我認爲這意味着在性能上受到打擊。無論如何,我找到了一個解決方案,無需嵌套,我發佈它作爲一個單獨的答案,以防你想檢查它。無論如何感謝您的幫助! – kace91 2014-11-01 14:20:38

0

我找到了解決自己。訣竅是在底部設置兩個元素,然後將編輯文本設置爲寬度:0dp,alignParentLeft和toLeftOF按鈕(不是按鈕的左對齊,而是左對齊按鈕)。該按鈕應該與父母權利和父母權利對齊。

最終的代碼如下所示:

<EditText 
     android:id="@+id/edittext" 

     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_toLeftOf="@id/button" 
     android:layout_alignParentLeft="true" 
     android:hint="@string/editText_Hint" 
     android:inputType="number" 
     android:layout_width="0dp" 
     > 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:onClick="sendAnswer" 
     android:text="@string/button_text" /> 
相關問題