2016-08-22 30 views
1

我想在較低的android系統上使用更高api的xml屬性。如何做到這一點,而不會改變太多?如android:layout_columnWeightandroid:layout_rowWeigh。 這裏是我的XML代碼:如何在較低的api上使用更高api的xml屬性?

<GridLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_column="0" 
    android:layout_row="0" 
    android:layout_gravity="fill" 
    android:layout_columnWeight="1" 
    android:layout_rowWeight="1" 
    android:text="Hello" 
    android:id="@+id/hello" 
    android:onClick="buttonTapped"/> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_column="1" 
    android:layout_row="0" 
    android:layout_gravity="fill" 
    android:layout_columnWeight="1" 
    android:layout_rowWeight="1" 
    android:text="How are you" 
    android:id="@+id/howareyou" 
    android:onClick="buttonTapped"/> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_column="0" 
    android:layout_row="1" 
    android:layout_gravity="fill" 
    android:layout_columnWeight="1" 
    android:layout_rowWeight="1" 
    android:text="good evening" 
    android:id="@+id/goodevening" 
    android:onClick="buttonTapped"/> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_column="1" 
    android:layout_row="1" 
    android:layout_gravity="fill" 
    android:layout_columnWeight="1" 
    android:layout_rowWeight="1" 
    android:text="please" 
    android:id="@+id/please" 
    android:onClick="buttonTapped"/> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_column="0" 
    android:layout_row="2" 
    android:layout_gravity="fill" 
    android:layout_columnWeight="1" 
    android:layout_rowWeight="1" 
    android:text="my name is" 
    android:id="@+id/mynameis" 
    android:onClick="buttonTapped"/> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_column="1" 
    android:layout_row="2" 
    android:layout_gravity="fill" 
    android:layout_columnWeight="1" 
    android:layout_rowWeight="1" 
    android:text="do you &#10;speak english" 
    android:id="@+id/doyouspeakenglish" 
    android:onClick="buttonTapped"/> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_column="0" 
    android:layout_row="3" 
    android:layout_gravity="fill" 
    android:layout_columnWeight="1" 
    android:layout_rowWeight="1" 
    android:text="welcome" 
    android:id="@+id/welcome" 
    android:onClick="buttonTapped"/> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_column="1" 
    android:layout_row="3" 
    android:layout_gravity="fill" 
    android:layout_columnWeight="1" 
    android:layout_rowWeight="1" 
    android:text="i live in" 
    android:id="@+id/ilivein" 
    android:onClick="buttonTapped"/> 

請不要說用另一種方法來做到這一點,網格佈局是非常有用的,但我不能用較低的Android系統。

+1

指定對GridLayout來說太低的目標API級別可能會有所幫助。它不是一個V7支持庫嗎? – emerssso

+0

這些屬性不與API級別綁定。這些屬性與容器有關。您可以使用[GridLayout.LayoutParams'](https://developer.android.com/reference/android/widget/GridLayout.LayoutParams.html)上定義的屬性,因爲您的小部件位於'GridLayout'內部。而且,如答案所述,如果您想在API Level 14之前使用'GridLayout',請使用'support-v4'庫中的'GridLayout' backport。 – CommonsWare

回答

0

只需從支持庫切換到GridLayout即可。

這樣你就可以在舊的API上使用你的layout_columnWeight,但用app替換android命名空間前綴。

Reference

唯一的另一種方法是使用替代佈局文件你layout-v21目錄或裏面的東西。

相關問題