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
Android relative layout problem with gravity
提问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_gravity
does not work with the subviews of a RelativeLayout
. It is for use with a LinearLayout
or FrameLayout
. However, the subviews of a RelativeLayout
can still use gravity
as 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
.
The gravity
works but layout_gravity
doesn'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_horizontal"。