2017-04-23 41 views
-2

我試圖在android中做GridLayout,但我並沒有屈服於按鈕是平等的,我想做的佈局分爲3部分,部分將顯示3個按鈕,並按鈕將顯示所有的屏幕。 有人可以幫助我的代碼?Android佈局不成功定位

+0

發佈您迄今爲止已完成的代碼。 – deejay

+0

我剛剛做了RelativeLayout,我把按鈕放在裏面,但它沒有很好地工作。 – mat

+0

你爲什麼使用RelativeLayout? – deejay

回答

0

在你的xml中試試這個。

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:columnWidth="90dp" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
/> 

這將創建一個與每個col等寬的gridview。您可以通過將numColumns指定爲所需的數字來設置列數。

要使用LinearLayout,您可以將子視圖的Layout_weight設置爲等值。

<LinearLayout 
    android:id="@+id/linearLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<TextView 
    android:id="@+id/tv1" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight = "1"/> 

<TextView 
    android:id="@+id/tv2" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight = "1"/> 

<TextView 
    android:id="@+id/tv3" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight = "1"/> 
</LinearLayout> 

你並不需要設置weightSum父佈局只是簡單的設置孩子layout_weight,不要忘記設置寬度或高度子視圖0dp。

設置爲layout_width0dp爲水平和layout_height0dp垂直。並根據需要將另一個設置爲match_parentwrap_content

+0

好的,謝謝! – mat

+0

如果我想用LinearLayout做到這一點與權衡,我如何劃分屏幕eqalus 3部分? – mat

+0

是的,你也可以使用LinearLayout。我將在答案中添加一個簡短的xml代碼。 – deejay