android:layout_width="0dip" 와 android:layout_weight="1" 였음..
설정 예제는..
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5px"
android:gravity="bottom"
android:stretchColumns="0,1"
>
<TableRow>
<ImageButton
android:id="@+id/n11"
android:layout_column="0"
android:layout_width="0dip"
android:layout_height="38dip"
android:scaleType="fitXY"
android:layout_weight="1"
android:background="#ffffff"
android:src="@drawable/plusbtn"
/>
적용하니.. 원하는대로 버튼의 가로 크기가 균일하게 나옴.. ^^
왜 이렇게 하면 폭이 같아지나요?
LinearLayout과 TableRow는 자식 뷰의 크기를 먼저 정한 뒤 남는 공간을 layout_weight 비율대로 나눠 줍니다. layout_width를 0dip으로 두면 각 버튼의 기본 폭이 0이 되어, 남는 공간 전체가 weight 비율(여기서는 모두 1)대로 분배됩니다. 그래서 버튼 안의 이미지나 글자 크기와 상관없이 같은 폭이 됩니다. 반대로 wrap_content로 두면 내용물 크기가 먼저 반영되어 폭이 서로 달라질 수 있습니다.
확인 방법
- 버튼 개수를 늘리거나 이미지 크기를 다르게 바꿔도 폭이 같은지 에뮬레이터에서 확인합니다.
- weight를 하나만 2로 바꾸면 그 버튼이 다른 버튼의 두 배 폭을 차지하는지 확인해 비율 분배를 이해할 수 있습니다.
요즘 방식으로 쓴다면
- fill_parent는 API 8부터 match_parent로 대체되었으므로 새 코드에서는 match_parent를 사용합니다. dip과 dp는 같은 단위입니다.
- 한 줄에 같은 폭으로 나열하려면 가로 방향 LinearLayout에 자식 폭 0dp와 layout_weight 1을 주는 방식을 그대로 쓸 수 있습니다.
- 행과 열이 있는 격자 형태라면 GridLayout(API 21 이상)의 layout_columnWeight, 또는 ConstraintLayout의 가로 체인(chainStyle spread)과 0dp 폭 조합을 고려할 수 있습니다.
이 글은 2011년 기준으로 작성되었고, 위 보강 설명은 2026년 11월에 추가했습니다.
English version: Android TableLayout: Make Button Widths Equal with layout_weight
댓글 없음:
댓글 쓰기