[英]how to center align a text inside textView in RelativeLayout with a sibling
I have code below which center aligns the text inside a TextView but its a hack. 我有下面的代码,该代码的中心将TextView内的文本对齐,但这是一个问题。 Please see my comment in between. 请参阅我的评论。
Is there a way I can achieve the same result but without the hack? 有没有办法能达到相同的结果但又不会被黑客入侵?
Please note I want text to be center aligned wrt. 请注意,我希望文字在中心对齐。 Screen irrespective of the width of the sibling (in case below 'action_back') 屏幕,与兄弟姐妹的宽度无关(在“ action_back”以下的情况下)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="@+id/action_back"
layout="@layout/action_back"
android:layout_height="match_parent"
android:layout_width="wrap_content" />
<!--
TODO:
hack TextView width is set as match_parent to
make it center align with Screen dimensions,
which overlaps with back btn. I could
not find another way to center it.
-->
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:layout_centerInParent="true"
android:gravity="center"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="17sp"
android:textStyle="bold" />
</RelativeLayout>
Have you really tried keeping TextView width as 您是否真的尝试过将TextView宽度保持为
android:layout_width="wrap_content"
and 和
android:layout_centerInParent="true"
together? 一起? Because other than 因为除了
android:layout_width="match_parent" android:layout_width =“ match_parent”
in your TextView I could not see any issue. 在您的TextView中,我看不到任何问题。
There is one thing to note if your Title text is long it will overlap your button. 有一点要注意,如果您的标题文本很长,它将与您的按钮重叠。 To rectify it put 要纠正它
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="@id/action_back"
If you set the layout_width
to wrap_content
and android:layout_centerHorizontal="true"
you should be able to center the text in the Relative Layout. 如果您将layout_width
设置为wrap_content
并且android:layout_centerHorizontal="true"
您应该能够在相对布局中将文本居中。
EDIT: Maybe something like this: 编辑:也许是这样的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="@+id/action_back"
layout="@layout/action_back"
android:layout_height="match_parent"
android:layout_width="wrap_content" />
<LinearLayout
android:layout_toRightOf="@id/action_back"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="17sp"
android:textStyle="bold"/>
</LinearLayout>
</RelativeLayout>
just swap the positions of the tags. 只需交换标签的位置即可。 Rest seems fine to me. 休息对我来说似乎很好。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:gravity="center"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="17sp"
android:textStyle="bold" />
<include android:id="@+id/action_back"
layout="@layout/action_back"
android:layout_height="match_parent"
android:layout_width="wrap_content" />
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.