九宫格排按钮,内容不被拉伸(TableLayout、layout_weight、layout_gravity和gravity)

近日在使用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表示组件的子组件在组件中的位置

实现效果图如下:
这里写图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值