近日在使用TableLayout进行布局时,发现如果想让按钮不被拉伸的排列在TableLayout里面,单单使用TableLayout的stretchColumns和gravity属性怎么也达不到很好的理想状态。现变换下实现方法记录如下:
[shape.xml]
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充的颜色 -->
<solid android:color="#5dff00dd" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="20dip" />
<stroke
android:width="2dp"
android:color="#848484" />
</shape>
[activity_main.xml]
<FrameLayout
android:layout_width="300dip"
android:layout_height="300dip" >
<com.imageviewsvg.controls.ImageViewSvg
android:id="@+id/svgvie"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="#7fffffff"
android:src="@drawable/lion" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow
android:id="@+id/above"
android:layout_weight="1">
<Button
android:layout_width="50dip"
android:layout_height="50dip"
android:background="@drawable/shape"
android:text="左上"
android:layout_gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<Button
android:id="@+id/button1"
android:layout_width="50dip"
android:layout_height="50dip"
android:background="@drawable/shape"
android:text="上"/>
</LinearLayout>
<Button
android:layout_width="50dip"
android:layout_height="50dip"
android:background="@drawable/shape"
android:text="右上"
android:layout_gravity="center" />
</TableRow>
<TableRow
android:id="@+id/middle"
android:layout_weight="1">
<Button
android:layout_width="50dip"
android:layout_height="50dip"
android:background="@drawable/shape"
android:text="左"
android:layout_gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<Button
android:layout_width="50dip"
android:layout_height="50dip"
android:background="@drawable/shape"
android:text="中" />
</LinearLayout>
<Button
android:layout_width="50dip"
android:layout_height="50dip"
android:background="@drawable/shape"
android:text="右"
android:layout_gravity="center"/>
</TableRow>
<TableRow
android:layout_weight="1">
<Button
android:layout_width="50dip"
android:layout_height="50dip"
android:background="@drawable/shape"
android:text="左下"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<Button
android:layout_width="50dip"
android:layout_height="50dip"
android:background="@drawable/shape"
android:text="下" />
</LinearLayout>
<Button
android:layout_width="50dip"
android:layout_height="50dip"
android:background="@drawable/shape"
android:text="右下"
android:layout_gravity="center" />
</TableRow>
</TableLayout>
</FrameLayout>
当然,里面有很多重复性的代码(可以使用include方法进行优化),在此没有进行优化。
在此需要进行总结说明的有2点:
1、想上下平分各TableRow高度时最好使用layout_weight属性
2、layout_gravity和gravity的区别:
-------->layout_gravity 表示组件自身在父组件中的位置
-------->gravity表示组件的子组件在组件中的位置
实现效果图如下: