在TableRow中创建宽度相同的TextViews



我面临一个问题:我想为我的主屏幕创建一个图标网格,所以我在每个屏幕中使用4个TableRows和3个TextViewsTextViews由一个图标和一个文本组成,文本具有不同的长度。

我不知道该在xml文件中写什么,以使所有TextViews在一行中具有相同的宽度。我在logcat中显示了它们的大小,所以我确信它们的宽度不相同;(

我试过m1shk4的解决方案,但没有用。

<?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>

知道吗?感谢:(

由于TableRow表示水平方向的LinearLayout,因此权重属性应与layout_width="0dp"一起用于子视图。因此为了使CCD_ 12中的每个CCD_

android:layout_width="match_parent"

带有

android:layout_width="0dp"

对于一行中的每个CCD_ 13。

最新更新