我想爲xFragment類進行佈局。這個佈局必須是match_parent的寬度和高度。在xml中進行佈局,可以通過編程方式添加項目
圖片1
在圖像1中,佈局四個項目編程加入...
圖片2
在畫面2是同一佈局但有6個項目。
這些佈局示例都不是可滾動的,所以如果我添加8個項目,所有項目都必須適合 的屏幕大小。
我該怎麼做?
我想爲xFragment類進行佈局。這個佈局必須是match_parent的寬度和高度。在xml中進行佈局,可以通過編程方式添加項目
圖片1
在圖像1中,佈局四個項目編程加入...
圖片2
在畫面2是同一佈局但有6個項目。
這些佈局示例都不是可滾動的,所以如果我添加8個項目,所有項目都必須適合 的屏幕大小。
我該怎麼做?
您可以使用ScrollView。
如果您現在使用GridView,ScrollView將包含GridView。
或 如果使用ifContentScrolls模式,則使用滾動模式。
tableview呢? – andrescabana86
你的意思是TableLayout?幾乎佈局的父項是ViewGroup。所以你應該。我嘗試這個,這是成功。 – Amadas
可以在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>
你可以簡單地做到這一點:通過ID指的LinearLayout和編程添加子。如果您希望孩子擁有相同的身高,請將其高度設置爲0px,並將其重量設置爲相同,例如1。由於它的孩子也將是Linearlayouts,因此您可以將它們的項目manu1,menu2等添加到它們中,沒有任何問題。我不知道你確切的用法,但這是一個開始。
我認爲這可以幫助你:http://stackoverflow.com/questions/2963152/android-how-to-resize-a-custom-view-programmatically –
關閉但不相同,如果我使用TableView? – andrescabana86
使用GridView ... – Piyush