2017-08-15 76 views
2
<?xml version="1.0" encoding="utf-8"?> 

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

    <Button 
     android:id="@+id/btn1" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" /> 

    <Button 

     android:id="@+id/btn2" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_toRightOf="@+id/btn1"/> 

    <Button 
     android:id="@+id/btn2" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_toRightOf="@+id/btn1"/> 

     <!--x7--> 

我在其相關佈局中放置了7個按鈕。我想調整他們的'寬度'到相同的大小以適合屏幕。我應該怎麼辦RelativeLayout中的自動縮放按鈕的寬度

+0

是否有必要將relativeLayout作爲父項? –

+0

android:layout_width =「match_parent」不起作用? – Vega

+0

如果我可以做到這一點沒有relativeLayout,我不會把relativeLayout作爲父項。 –

回答

2

您可以使用LinearLayout作爲父母,可以給weightweightsum實現這一目標,使用相對佈局,可以不適合整個屏幕或它可能會超過基於設備上的屏幕。

你可以做到這一點使用的LinearLayout像下面,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:weightSum="7" 
       android:layout_height="match_parent"> 
    <Button 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 
+0

我懂了!感謝您的幫助。 –

+0

歡迎,如果你覺得它解決了你的問題,你可以標記爲答案。 –

0

如果你想顯示每個按鈕(高度= match_parent)的,然後使用滾動視圖。我希望這可以幫助