Android的:如何TableLayout列在中心观点? [英] Android: how to center view within column of TableLayout?

查看:109
本文介绍了Android的:如何TableLayout列在中心观点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个复选框电网TabeLayout。在顶部有2头,并且每个附加行由一个标签和2复选框。我想表填满所有可用的空间中,第一列被定向到左边,并且在复选框和他们的头部将其列内居中。在下面的布局,标题居中显示正常,但复选框都没有。我加了背景色的复选框中的一个,这表明复选框实际上增长了,以填补其列(这就是为什么它不会因为中心需要);尽管WRAP_CONTENT布局参数。

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =center_horizo​​ntal
    机器人:方向=垂直>
    < TableLayout
        机器人:stretchColumns =1,2
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>

        <的TableRow>
            <的TextView
                机器人:文本=
                机器人:重力=center_horizo​​ntal
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT>
            < / TextView的>
            <的TextView
                机器人:文本=第一类
                机器人:重力=center_horizo​​ntal
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT>
            < / TextView的>
            <的TextView
                机器人:文本=第二类
                机器人:重力=center_horizo​​ntal
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT>
            < / TextView的>
        < /的TableRow>
        <的TableRow>
            <的TextView
                机器人:文本=行1:
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT>
            < / TextView的>
            <复选框
                机器人:后台=#FF001122
                机器人:layout_height =50dip
                机器人:layout_width =WRAP_CONTENT
                机器人:重力=center_horizo​​ntal>
            < /复选框>
            <复选框
                机器人:layout_height =50dip
                机器人:layout_width =WRAP_CONTENT
                机器人:重力=center_horizo​​ntal>
            < /复选框>
        < /的TableRow>
    < / TableLayout>
< / LinearLayout中>
 

解决方案

,而不是设置上的复选框安卓重力=center_horizo​​ntal,你要设置安卓layout_gravity =center_horizo​​ntal

安卓重力控制视图如何勾画出它的内容里面自己的容器,因为你必须设置为WRAP_CONTENT宽度,没有为复选框,中心本身的内部没有任何空隙。

的android:layout_weight 告诉父视图(在这种情况下的TableRow)如何定位子,所以将围绕表格单元格内的复选框。

I am trying to create a TabeLayout with a grid of CheckBoxes. Across the top are 2 headers, and each additional row consists of a label and 2 CheckBoxes. I would like the table to fill all available space, the first column to be oriented to the left, and the CheckBoxes and their headers to be centered within their columns. In the below layout, the headers appear centered properly, but the CheckBoxes are not. I added a background color to one of the CheckBoxes, this demonstrates that the CheckBox actually grew to fill its column (which is why it won't center as desired); despite the "wrap_content" layout param.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical">
    <TableLayout 
        android:stretchColumns="1,2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TableRow>
            <TextView 
                android:text="" 
                android:gravity="center_horizontal"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </TextView>
            <TextView 
                android:text="First category" 
                android:gravity="center_horizontal"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </TextView>
            <TextView 
                android:text="2nd category" 
                android:gravity="center_horizontal"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </TextView>
        </TableRow>
        <TableRow>
            <TextView 
                android:text="Row 1: " 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </TextView>
            <CheckBox
                android:background="#FF001122"
                android:layout_height="50dip"
                android:layout_width="wrap_content" 
                android:gravity="center_horizontal">
            </CheckBox>
            <CheckBox
                android:layout_height="50dip"
                android:layout_width="wrap_content" 
                android:gravity="center_horizontal">
            </CheckBox>
        </TableRow>
    </TableLayout>
</LinearLayout>

解决方案

On the CheckBox, instead of setting android:gravity="center_horizontal", you want to set android:layout_gravity="center_horizontal"

android:gravity controls how the view lays out its contents inside its own container, and since you have the width set to "wrap_content", there is no void space for the CheckBox to center itself inside of.

android:layout_weight tells the parent view (in this case the TableRow) how to position the child, so it will center the CheckBox inside the table cell.

这篇关于Android的:如何TableLayout列在中心观点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆