Android 重力相对布局问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2804411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:28:21  来源:igfitidea点击:

Android relative layout problem with gravity

androidlayout

提问by DixieFlatline

How can i put textview with id="naslov" to the center? I also tried with layout_gravity="center" but that doesn't work either.

如何将带有 id="naslov" 的 textview 放在中心?我也尝试过 layout_gravity="center" 但这也不起作用。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView   
    android:id="@+id/naslov"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20dip"
    android:text="Povzetek"
    android:gravity="center"/>
   <TextView   
    android:id="@+id/aha"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="10dip"
    android:text="Vseh oddaj:"
       android:layout_below="@id/naslov"/>  
 </RelativeLayout> 

回答by

 android:layout_centerHorizontal="true"

回答by Alex Volovoy

 android:layout_centerHorizontal="true"

there is also

还有

android:layout_centerInParent="true"

Full list for RelativeLayout attributes is here

相对布局属性的完整列表在这里

Also you can tell your TextView to fill_parent and then set gravity=center on it. So it'll center actual text within textView.

你也可以告诉你的 TextView 到 fill_parent 然后在它上面设置引力 = 中心。所以它会将实际文本放在 textView 中。

回答by Suragch

Supplemental Answer

补充答案

As a more general answer to this question, layout_gravitydoes not work with the subviews of a RelativeLayout. It is for use with a LinearLayoutor FrameLayout. However, the subviews of a RelativeLayoutcan still use gravityas usual because this is just how a view arranges its own content.

作为对这个问题的更一般的答案,layout_gravity不适用于RelativeLayout. 它与 aLinearLayout或一起使用FrameLayout。但是,a 的子视图RelativeLayout仍然可以照常使用gravity,因为这正是视图安排自己内容的方式。

See the comparison in the following image. The green and blue views are TextViews inside of a RelativeLayout.

请参阅下图中的比较。绿色和蓝色视图是RelativeLayout.

enter image description here

在此处输入图片说明

The gravityworks but layout_gravitydoesn't. See my fuller answerfor more details.

gravity工作,但layout_gravity没有。有关更多详细信息,请参阅我的更完整答案

See also

也可以看看

回答by Abhishek Garg

Relative Layout does not make use of layout_gravity, however you use gravity values in following ways : center, left , left|center , right|center , right, top, bottom.

相对布局不使用 layout_gravity,但是您可以通过以下方式使用重力值:center、left、left|center、right|center、right、top、bottom。

<!-- below widget displaying text in left center -->
<TextView
           android:id="@+id/song_title"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:text="sample name text"
           android:gravity="left|center"
           android:textSize="15sp" />

回答by Bobby

You can go to the graphical layout tab in the activity_main.XML and select you item you want to position and change it from the three tabs on the top. Or you can just put this: android:layout_gravity="center_horizontal".

您可以转到 activity_main.XML 中的图形布局选项卡,然后从顶部的三个选项卡中选择要定位和更改的项目。或者你可以这样写:android:layout_gravity="center_horizo​​ntal"。