I'm facing a problem: I'd like to create a grid of icons for my main screen and so I'm using 4 TableRows
and 3 TextViews
in each.
The TextViews
are made of an icon and a text, the text is of different length.
I don't know what to write in the xml
file to make all those TextViews
the same width in a row.
I displayed their size in logcat
so I'm sure they're not of the same width ;)
I've tried m1shk4
's solution, but that doesn't work.
<?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 & 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>
Any idea? Thanks :)
Since TableRow
represents a horizontally oriented LinearLayout
, the weight attribute should be used with layout_width="0dp"
for the child views. So in order to make each TextView
in the TableRow
be same width replace
android:layout_width="match_parent"
with
android:layout_width="0dp"
for every TextView
in a row.