我想要一个3行4列的tablellayout。
我用一款5.5屏幕大小的手机(三星Galaxy Note 2)试过了,两行的高度几乎相同,但在一款9.6英寸的平板电脑(三星Galaxy Tab E)上,最后一行非常小。我做错了什么?
我还附上了我的布局文件和实际布局的截图。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/domestic" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="5sp" android:gravity="center" android:background="@drawable/rounded_corner_start"> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:measureWithLargestChild="true" android:stretchColumns="*"> <!-- Row with 4 columns --> <TableRow android:layout_height="0dp" android:layout_width="match_parent" android:layout_weight="1"> <!--Kutya--> <LinearLayout android:id="@+id/lnDog" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_margin="3sp" android:orientation="vertical" android:background="@drawable/rounded_corner_elements" android:layout_weight="1"> <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.8" android:src="@drawable/dog"/> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:fontFamily="sans-serif-thin|bold" android:text="@string/dog" android:gravity="center" android:layout_weight="0.2" /> </LinearLayout> <!--Macska--> <LinearLayout android:id="@+id/lnCat" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_margin="3sp" android:orientation="vertical" android:background="@drawable/rounded_corner_elements" android:layout_weight="1"> <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.8" android:src="@drawable/cat2"/> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:fontFamily="sans-serif-thin|bold" android:text="@string/cat" android:gravity="center" android:layout_weight="0.2" /> </LinearLayout> <!--Diszno--> <LinearLayout android:id="@+id/lnPig" android:layout_width="0dp" android:layout_margin="3sp" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/rounded_corner_elements" android:layout_weight="1"> <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.8" android:src="@drawable/pig"/> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:fontFamily="sans-serif-thin|bold" android:text="@string/pig" android:gravity="center" android:layout_weight="0.2" /> </LinearLayout> <!--Tyuk--> <LinearLayout android:id="@+id/lnChicken" android:layout_width="0dp" android:layout_margin="3sp" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/rounded_corner_elements" android:layout_weight="1"> <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.8" android:src="@drawable/chicken"/> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:fontFamily="sans-serif-thin|bold" android:text="@string/chicken" android:gravity="center" android:layout_weight="0.2" /> </LinearLayout> </TableRow> .... 4 times </TableLayout> </LinearLayout> </LinearLayout>
所以,最后我发现所有的图像应该有相同的高度,然后所有的行都有相同的高度。这很奇怪,因为我本以为通过设置tablerow的权重,内部内容会自动调整自身大小。