2013-09-22 60 views
1

我正在創建一個反饋表單,最後有兩個按鈕「清除」和「發送」。這必須在相對佈局內完成。相對佈局中的格式按鈕大小

問題是按鈕不夠寬,我想讓按鈕匹配上面的寬度。

下面是應用程序試圖改變寬度之前:

enter image description here

和代碼:

<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="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/feedback_name_hint" > 
    </EditText> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/editText1" 
     android:hint="@string/feedback_email_hint" > 
    </EditText> 

    <EditText 
     android:id="@+id/editText3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/ratingBar1" 
     android:layout_alignLeft="@+id/editText2" 
     android:layout_alignRight="@+id/editText2" 
     android:layout_below="@+id/editText2" 
     android:gravity="center_vertical|top" 
     android:hint="@string/feedback_actual" 
     android:inputType="textMultiLine" > 

    </EditText> 

    <RatingBar 
     android:id="@+id/ratingBar1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/Button01" 
     android:layout_alignLeft="@+id/editText3" 
     android:layout_marginBottom="21dp" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/ratingBar1" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="18dp" 
     android:text="@string/Clear" /> 

    <Button 
     android:id="@+id/Button01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button1" 
     android:layout_alignBottom="@+id/button1" 
     android:layout_toRightOf="@+id/button1" 
     android:text="@string/Send" /> 


</RelativeLayout> 

我試圖圍繞按鈕創建一個線性佈局,像這樣:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="top" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="@string/Clear" 
    /> 
    <Button 
     android:id="@+id/button4" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="@string/Send" 
    /> 
</LinearLayout> 

但是,雖然按鈕是我想要的大小,就跳轉到視圖的頂部,並影響其他元素:

enter image description here

任何幫助將不勝感激,謝謝。

解決:

它的工作由surrouding按鈕與下面的線性佈局:(注意評級酒吧是如何設置爲高於linearlayoutid

<RatingBar 
     android:id="@+id/ratingBar1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/linearlayout_id" 
     android:layout_alignLeft="@+id/editText3" 
     android:layout_marginBottom="21dp" /> 

<LinearLayout 
    android:id="@+id/linearlayout_id" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:gravity="center_horizontal" 
     android:layout_marginBottom="18dp" 
     android:orientation="horizontal" > 

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

     <Button 
      android:id="@+id/Button01" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/Send" /> 
</LinearLayout> 

回答

1

試試這個 用下面的代碼替換你的xml代碼。

在這裏,我通過給android:layout_weight="1"到兩個按鈕

<RatingBar 
     android:id="@+id/ratingBar1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/linearlayout_id" 
     android:layout_alignLeft="@+id/editText3" 
     android:layout_marginBottom="21dp" /> 

    <LinearLayout 
     android:id="@+id/linearlayout_id" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:gravity="center_horizontal" 
     android:layout_marginBottom="18dp" 
     android:orientation="horizontal" > 

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

     <Button 
      android:id="@+id/Button01" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@String/Send" /> 
    </LinearLayout> 

添加android:layout_above="@+id/linearlayout_id"RatingBar,並把你的按鈕LinearLayout這應該工作

0

你把它們放在小的嘗試LinearLayout是正確的。只需將android:layout_alignParentBottom="true"添加到LinearLayout,它應該適合您。

更新: 另外,在LinearLayout中將android:layout_height="fill_parent"更改爲android:layout_height="wrap_content"

+1

我想這一點,但它沒有工作:( – Tom

+1

能你發佈了更新後的代碼(用我的答案),另外,確保你清理你的項目,因爲XML佈局有時會有不正確的重新編譯的傾向 – JRomero

+1

我想我發現了另外的問題,se更新了答案 – JRomero