android:layout_gravity为什么不能让按钮居中显示?

iceli 2014-03-19 04:17:49
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow >
<TextView android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/TextView01"
android:text="Test Toast"/>
</TableRow>
<TableRow >
<Button android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Button01"
android:text="LONG"/>
</TableRow>
<TableRow >
<Button android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Button02"
android:text="SHORT"/>
</TableRow>
</TableLayout>
</RelativeLayout>

为什么 <TableRow >
<Button android:layout_gravity="center_horizontal"
不能使按钮居中显示?
而<TableRow android:gravity="center">
可以使按钮居中显示?
android:gravity:
这个是针对控件里的元素来说的,用来控制元素在该控件里的显示位置。这个显示是对的。
android:layout_gravity:
这个是针对控件本身而言,用来控制该控件在包含该控件的父控件中的位置。这个怎么不能控制Button在TableRow中的位置?
...全文
3440 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
jxzzgs111 2016-07-12
  • 打赏
  • 举报
回复
http://my.oschina.net/gef/blog/542891 看一下这个,希望对你有帮助
  • 打赏
  • 举报
回复
android:layout_gravity="center_horizontal" ,没错,加 了layout是view本身相对于父控件,你那个其实已经居中显示了,没有效果出来的原因是你要给你的父控件设置它的实际宽度,因为没有设置的话,它就相当于包裹内容,所以看上去就没有什么效果了,这是我的理解,你可以尝试下。。。
dai_jiawei 2015-03-30
  • 打赏
  • 举报
回复
其实主要是因为你的TableLayout它的宽度是fill_parent的。而包在它里面的控件的大小是wrap_content的。所以用gravity = "center"针对的是整个屏幕的宽度,所以它包含中的按钮可以按其自适应宽度居中。而你按钮是wrap_content的,然后用layout_gravity,它是对应该按钮的当前的自适应宽度而并非整个屏幕宽度,所以它并不会出现居中显示。
jiang920627 2015-03-30
  • 打赏
  • 举报
回复
Lz 你这么纠结这个问题干什么?能解决问题就行了。
山鹰1985 2015-01-21
  • 打赏
  • 举报
回复
相对布局,顾名思义,通俗点说就是相对于哪个的哪边;比如下面3个,第一个全部居中,第二个水平居中,第三个垂直居中;
android:layout_centerInParent="true" 
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
梁柯一夏 2015-01-20
  • 打赏
  • 举报
回复
对于 LinearLayout 当 android:orientation="vertical" 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。 当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。 参考自该博文:http://www.cnblogs.com/xiaoQLu/archive/2012/10/22/2733631.html
jake_wu_1987 2014-03-31
  • 打赏
  • 举报
回复
试试这个: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="0" > <TableRow > <TextView android:layout_gravity="left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/TextView01" android:text="Test Toast"/> </TableRow> <TableRow > <Button android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Button01" android:text="LONG"/> </TableRow> <TableRow > <Button android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Button02" android:text="SHORT"/> </TableRow> </TableLayout> </RelativeLayout>
iceli 2014-03-31
  • 打赏
  • 举报
回复
引用 10 楼 cclovescw 的回复:
TableRow默认就是有多列的,你设置了<TableRow android:gravity="center">,就让你的列都居中了。
如果你只设置<Button android:layout_gravity="center_horizontal",那它只在第一列的位置移动。你可以试试
<Button android:layout_gravity="left",<Button android:layout_gravity="right",
应该会有移动距离,但肯定限制在第一列里



一个设置了left,一个设置了right,为啥两个都在左边呢?
  • 打赏
  • 举报
回复
TableRow默认就是有多列的,你设置了<TableRow android:gravity="center">,就让你的列都居中了。 如果你只设置<Button android:layout_gravity="center_horizontal",那它只在第一列的位置移动。你可以试试 <Button android:layout_gravity="left",<Button android:layout_gravity="right", 应该会有移动距离,但肯定限制在第一列里
领带有点歪 2014-03-28
  • 打赏
  • 举报
回复
澳~我有点明白了,LinearLayout本身就是线性布局,它本身的对子控件的居中方式只能有一种,不能像RelativeLayout那么灵活
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#ff8989" >

    <LinearLayout
        android:background="#000000"
         android:layout_gravity="center_vertical"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:orientation="horizontal" >

        <Button
            android:layout_gravity="center_vertical"
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        
    </LinearLayout>

</LinearLayout>
向上边这个center_horizontal居中就没用;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#ff8989" >

    <LinearLayout
        android:background="#000000"
         android:layout_gravity="center_vertical"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:orientation="vertical" >

        <Button
            android:layout_gravity="center_horizontal"
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        
    </LinearLayout>

</LinearLayout>
这个就是"center_vertical"没用,, 嘴比较笨,心里敞亮但说不明白~见谅哈
领带有点歪 2014-03-28
  • 打赏
  • 举报
回复
我不是很清楚~瞎猜的,是不是因为TableRow 继承是LinearLayout,而LinearLayout本身就不支持layout_gravity="center_horizontal";测试LinearLayout"center_horizontal"没用,但是"center_vertical"有用
iceli 2014-03-28
  • 打赏
  • 举报
回复
没人知道吗?
iceli 2014-03-25
  • 打赏
  • 举报
回复
引用 4 楼 cybbz 的回复:
<TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="cente"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Button01" android:text="LONG"/> </TableRow> 貌似你的在布局里设置吧
我问的是为什么 <TableRow > <Button android:layout_gravity="center_horizontal" 不能使按钮居中显示?
欧姆猴子 2014-03-24
  • 打赏
  • 举报
回复
应该是控制里面的子控件的
欧姆猴子 2014-03-24
  • 打赏
  • 举报
回复
<TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="cente"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Button01" android:text="LONG"/> </TableRow> 貌似你的在布局里设置吧
  • 打赏
  • 举报
回复
应该是布局的问题吧,试试LinearLayout
jake_wu_1987 2014-03-21
  • 打赏
  • 举报
回复
在tablelayout里加android:stretchColumns="0",就ok了
android_Y 2014-03-19
  • 打赏
  • 举报
回复
我测试了下,TableRow TableLayout都无效 RelativeLayout中的控件没有android:layout_gravity LinearLayout和FrameLayout有效果 为什么我就不知道了

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧