2

我遇到了一个问题:我想为我的主屏幕创建一个图标网格,所以我分别使用 4TableRows和 3 TextViews。由TextViews图标和文本组成,文本的长度不同。

我不知道在xml文件中写什么以使所有这些都TextViews具有相同的宽度。我显示了它们的尺寸,logcat所以我确定它们的宽度不一样;)

我已经尝试过m1shk4's solution,但这不起作用。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_weight="1"
        android:gravity="center" >

            <TextView
                android:id="@+id/textView1"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:clickable="true"
                android:drawableTop="@drawable/nearby"
                android:gravity="center"
                android:onClick="goToArticles"
                android:text="All Nearby" />

            <TextView
                android:id="@+id/textView2"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:clickable="true"
                android:layout_width="match_parent"
                android:drawableTop="@drawable/supermarket"
                android:gravity="center"
                android:onClick="goToMap"
                android:text="Supermarkets" />

            <TextView
                android:id="@+id/textView3"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:drawableTop="@drawable/pharmacy"
                android:gravity="center"
                android:text="Pharmacy" />

    </TableRow>

   <TableRow
        android:id="@+id/tableRow2"
        android:layout_weight="1"
        android:gravity="center" >

            <TextView
                android:id="@+id/textView4"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:drawableTop="@drawable/fastfood"
                android:gravity="center"
                android:text="Fastfood" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:clickable="true"
                android:drawableTop="@drawable/departmentstores"
                android:gravity="center"
                android:onClick="displaySizes"
                android:text="Department Stores" />

            <TextView
                android:id="@+id/textView6"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:drawableTop="@drawable/petrol"
                android:gravity="center"
                android:text="Petrol Stations" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_weight="1"
        android:gravity="center" >

            <TextView
                android:id="@+id/textView7"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawableTop="@drawable/electronics"
                android:gravity="center"
                android:layout_width="match_parent"
                android:text="Electronics" />

            <TextView
                android:id="@+id/textView8"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:drawableTop="@drawable/diy"
                android:gravity="center"
                android:text="DIY Stores" />

            <TextView
                android:id="@+id/textView9"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:drawableTop="@drawable/banking"
                android:gravity="center"
                android:text="Banks" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_weight="1"
        android:gravity="center" >

            <TextView
                android:id="@+id/textView10"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:drawableTop="@drawable/fashion"
                android:gravity="center"
                android:text="Fashion &amp; Clothing" />

            <TextView
                android:id="@+id/textView11"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:drawableTop="@drawable/autoparts"
                android:gravity="center"
                android:text="Auto Service" />

            <TextView
                android:id="@+id/textView12"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:drawableTop="@drawable/more"
                android:gravity="center"
                android:text="All Categories" />

    </TableRow>

</TableLayout>

任何想法?谢谢 :)

4

1 回答 1

2

由于TableRow代表一个水平方向的LinearLayout权重属性应该与layout_width="0dp"子视图一起使用。因此,为了使每个都TextView具有TableRow相同的宽度,请替换

android:layout_width="match_parent"

android:layout_width="0dp"

连续的每TextView一个。

于 2014-06-04T20:01:00.250 回答