레이블이 Android인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Android인 게시물을 표시합니다. 모든 게시물 표시

2026년 11월 7일 토요일

[English] Android TableLayout: Make Button Widths Equal with layout_weight

Korean original: [TIP] Table Layout 의 Width 균일하게 설정

The problem

You have several buttons in a TableRow and want every button to have exactly the same width, no matter how large each image or label is.

The fix

Give each child view a width of 0dp and a layout_weight of 1.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/plusbtn" />
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/plusbtn" />
</TableRow>
</TableLayout>

Why this works

TableRow and LinearLayout measure each child first and then hand out the remaining space in proportion to layout_weight. With a width of 0dp the starting size of every child is zero, so the whole row width is shared by weight (here 1 : 1). With wrap_content, the content size is counted first, so widths can end up different.

How to check

  • Add more buttons or use images of different sizes. The widths should stay equal.
  • Set one weight to 2. That button should become twice as wide as the others.

Modern alternatives

  • fill_parent was replaced by match_parent in API level 8. The old dip unit is the same as dp.
  • For a single row, a horizontal LinearLayout with 0dp widths and weight 1 works the same way.
  • For a grid, consider GridLayout (API 21+) with layout_columnWeight, or a ConstraintLayout horizontal chain (spread) with 0dp widths.

Originally written in 2011 in Korean. This English version was added in November 2026.