2014-03-07 22 views
0

我想爲xFragment類進行佈局。這個佈局必須是match_parent的寬度和高度。在xml中進行佈局,可以通過編程方式添加項目

圖片1

enter image description here

在圖像1中,佈局四個項目編程加入...

圖片2

enter image description here

在畫面2是同一佈局但有6個項目。

這些佈局示例都不是可滾動的,所以如果我添加8個項目,所有項目都必須適合 的屏幕大小。

我該怎麼做?

+0

我認爲這可以幫助你:http://stackoverflow.com/questions/2963152/android-how-to-resize-a-custom-view-programmatically –

+0

關閉但不相同,如果我使用TableView? – andrescabana86

+0

使用GridView ... – Piyush

回答

0

您可以使用ScrollView。
如果您現在使用GridView,ScrollView將包含GridView。
或 如果使用ifContentScrolls模式,則使用滾動模式。

+0

tableview呢? – andrescabana86

+0

你的意思是TableLayout?幾乎佈局的父項是ViewGroup。所以你應該。我嘗試這個,這是成功。 – Amadas

0

可以在LinearLayout如下做到這一點使用layout_weight屬性...

<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="0dip" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <TextView 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#000000" 
      android:text="Menu 1" /> 

     <TextView 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#FFFFFF" 
      android:text="Menu 2" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <TextView 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#FFFFFF" 
      android:text="Menu 1" /> 

     <TextView 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#000000" 
      android:text="Menu 2" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <TextView 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#000000" 
      android:text="Menu 3" /> 

     <TextView 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#FFFFFF" 
      android:text="Menu 4" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <TextView 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#FFFFFF" 
      android:text="Menu 5" /> 

     <TextView 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#000000" 
      android:text="Menu 6" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <TextView 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#000000" 
      android:text="Menu 7" /> 

     <TextView 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#FFFFFF" 
      android:text="Menu 8" /> 
    </LinearLayout> 

</LinearLayout> 
0

你可以簡單地做到這一點:通過ID指的LinearLayout和編程添加子。如果您希望孩子擁有相同的身高,請將其高度設置爲0px,並將其重量設置爲相同,例如1。由於它的孩子也將是Linearlayouts,因此您可以將它們的項目manu1,menu2等添加到它們中,沒有任何問題。我不知道你確切的用法,但這是一個開始。

相關問題