2016-02-22 34 views
0

我有2個ImageButton和一個填充它的父級的相對佈局中的ToggleButton。以下是xml代碼:當沒有空間時自動更改相對佈局中的按鈕高度

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


     <ImageButton 
      android:layout_width="80dp" 
      android:layout_height="80dp" 
      android:id="@+id/imageButton4" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentStart="true" 
      android:src="@drawable/ic_exposure_minus1" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" /> 


     <ImageButton 
      android:layout_width="80dp" 
      android:layout_height="80dp" 
      android:id="@+id/imageButton6" 
      android:src="@drawable/ic_cancel_black" 
      android:layout_alignTop="@+id/imageButton4" 
      android:layout_toEndOf="@+id/imageButton4" 
      android:layout_marginLeft="5dp" /> 



     <ToggleButton 
      android:layout_width="80dp" 
      android:layout_height="80dp" 
      android:text="New ToggleButton" 
      android:id="@+id/toggleButton" 
      android:checked="false" 
      android:clickable="true" 
      android:textOn="+" 
      android:textOff="-" 
      android:textSize="18dp" 
      android:layout_below="@+id/imageButton4" 
      android:layout_toStartOf="@+id/imageButton6" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:layout_marginBottom="5dp" 
      android:layout_alignParentEnd="false" 
      android:layout_alignParentStart="false" /> 

    </RelativeLayout> 

當設備處於肖像模式時,按鈕有足夠的高度空間。當設備旋轉到橫向時,相對佈局變得更小,並且沒有空間容納所有按鈕。缺乏空間是在高度方面。因此,位於其他按鈕底部的Togglebutton會自動縮放到較小的高度,以便它適合。

我寧願所有按鈕的高度都會改變,而不是最後一個(ToggleButton)的高度變小。然後我會有3個具有相同高度的按鈕,而不是2個保持初始高度和1個較小高度的按鈕。 我可以通過更改xml文件來獲得這個結果嗎?

PS:爲了更清楚:

enter image description here

+0

如果您只爲橫向模式創建不同的佈局可能會更好。依靠自動調整大小的按鈕可能會導致按鈕非常難以點擊,如果他們的大小太小。 –

+0

你可以使用佈局權重 – saeed

回答

0

爲了讓3個按鍵高度相等,從相對佈局改變你的父母佈局線性佈局。並且同樣啓用佈局權重屬性。所以它們的尺寸相同。

+0

如果我使用線性佈局,那麼所有按鈕將在一行(垂直大小寫)或一列(水平大小寫)中。但我不希望他們那樣。 2個圖像按鈕位於另一個圖像按鈕的右側,第一個圖像按鈕下方的圖標按鈕位於右側。 – geo

0

如果您希望跨不同方向製作統一的用戶界面,則應使用LinearLayout。 使用定位財產連同佈局權重,你會得到你想要的結果。

你只需要做相應的嵌套Linearlayout。

這是使用線性佈局製作的佈局。

希望你明白使用linearlayouts的意義。

<?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"> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" 
       android:layout_marginTop="5dp" > 

      <ImageButton 
       android:layout_width="match_parent" 
       android:layout_height="80dp" 
       android:layout_weight="0.5" 
       android:id="@+id/imageButton4" 
       android:src="@drawable/ic_exposure_minus1" 
       android:layout_marginLeft="5dp"/> 


      <ImageButton 
       android:layout_width="match_parent" 
       android:layout_height="80dp" 
       android:layout_weight="0.5" 
       android:id="@+id/imageButton6" 
       android:src="@drawable/ic_cancel_black" 
       android:layout_marginLeft="5dp" /> 

      </LinearLayout> 

      <ToggleButton 
       android:layout_width="match_parent" 
       android:layout_height="80dp" 
       android:text="New ToggleButton" 
       android:id="@+id/toggleButton" 
       android:checked="false" 
       android:clickable="true" 
       android:textOn="+" 
       android:textOff="-" 
       android:textSize="18dp" 
       android:layout_marginTop="5dp" 
       android:layout_marginBottom="5dp"/> 

     </LinearLayout> 
相關問題