2014-08-29 93 views
0

我在LinearLayout中有6個按鈕。佈局居中。android layout自動調整按鈕大小以填充高度

是否可以讓按鈕在高度自動調整大小,以便每個按鈕的高度相同,但是所有按鈕的總和(加上它們之間的間距爲10dp)等於佈局的高度。

我需要這樣做,所以如果應用程序在屏幕較大或分辨率較高的設備上使用,它將縮放按鈕以填充佈局。

任何幫助和指導表示讚賞。

馬克

編輯代碼

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:orientation="vertical" 
     android:background="#ffffff" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp"> 

     <Button 
      android:id="@+id/button4" 
      android:layout_width="420px" 
      android:layout_height="99px" 
      android:layout_marginLeft="20sp" 
      android:layout_marginRight="20sp" 
      android:background="@drawable/butt1" 
      android:onClick="clk_raynes" 
      android:text="sitebutton" 
      android:layout_marginTop="10dp" 
      android:textColor="#000000" 
      android:textSize="50px" /> 


     <Button 
      android:id="@+id/rutlishbtn" 
      android:layout_width="420px" 
      android:layout_height="99px" 
      android:layout_marginLeft="20sp" 
      android:layout_marginRight="20sp" 
      android:layout_marginTop="6dp" 
      android:background="@drawable/butt1" 
      android:onClick="clk_rutlish" 
      android:text="Site2" 
      android:textColor="#000000" 
      android:textSize="50px" /> 


     <Button 
      android:id="@+id/harrisbtn" 
      android:layout_width="420px" 
      android:layout_height="99px" 
      android:layout_marginLeft="20sp" 
      android:layout_marginRight="20sp" 
      android:layout_marginTop="6dp" 
      android:background="@drawable/butt1" 
      android:onClick="clk_harris" 
      android:text="Site3" 
      android:textColor="#000000" 
      android:textSize="50px" /> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="420px" 
      android:layout_height="99px" 
      android:layout_marginLeft="20sp" 
      android:layout_marginRight="20sp" 
      android:layout_marginTop="6dp" 
      android:background="@drawable/butt1" 
      android:onClick="clk_ricards" 
      android:text="Site4" 
      android:textColor="#000000" 
      android:textSize="50px" /> 
     <Button 
      android:id="@+id/button10" 
      android:layout_width="420px" 
      android:layout_height="99px" 
      android:layout_marginLeft="20sp" 
      android:layout_marginRight="20sp" 
      android:layout_marginTop="6dp" 
      android:background="@drawable/butt1" 
      android:onClick="clk_btn10" 
      android:text="Site5" 
      android:textColor="#000000" 
      android:textSize="50px" /> 

     <Button 
      android:id="@+id/button11" 
      android:layout_width="420px" 
      android:layout_height="99px" 
      android:layout_marginLeft="20sp" 
      android:layout_marginRight="20sp" 
      android:layout_marginTop="6dp" 
      android:background="@drawable/butt1" 
      android:onClick="clk_btn11" 
      android:text="Site6" 
      android:textColor="#000000" 
      android:textSize="50px" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 
     </LinearLayout> 

     <Button 
      android:id="@+id/picbtn3" 

      android:layout_marginTop="6dp" 
      android:layout_marginBottom="190px" 

      android:text="³" 
      android:textColor="#000000" 
      android:textSize="30sp" 
      android:layout_width="60dp" 
      android:layout_height="60dp" 
      android:background="@drawable/butt1" 
      android:onClick="qrbtn" /> 

     </LinearLayout> 
     </ScrollView> 

回答

1

使用android:layout_height=0dpandroid:layout_weight=1在每個按鈕

我希望這可以幫助您!

+0

請參閱上面的編輯它已經在它,但它仍然不會改變 – user3422687 2014-08-29 19:50:52

+0

男子我得到它的工作,但我有一個問題picbtn3它需要是正方形,但當高度改變它變成一個矩形作爲寬度不會改變。我需要它來調整寬度是相同的高度我明白我不能這樣做在靜態XML但當我試圖改變活動的寬度我得到一個無法投射佈局錯誤任何想法 – user3422687 2014-08-30 09:48:38

+0

@ user3422687你可以以編程方式設置按鈕的高度和寬度: 'LinearLayout.LayoutParams params =(LinearLayout.LayoutParams)picButton3.getLayoutParams(); params.height = 100; params.width = 100; picButton3.setLayoutParams(params); ' 如果你給我看佈線錯誤的代碼行,我可以幫你:) – MartinCR 2014-08-31 17:40:23

0

嘗試在佈局xml中使用android:layout_weight = 1,並將LinearLayout方向設置爲垂直。使用按鈕上的邊距來創建所需的間隙。

相關問題