我在 RelativeLayout
中有一个简单的 TextView
。
我想将 TextView
的 end edge 定位到 RelativeLayout
的中心,如下图所示
我发现类似android:layout_toStartOf=...
例子:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf=...
/>
</RelativeLayout>
但这只会将结束边缘与开始边缘对齐。 (不能用于将结束边缘与布局的中心对齐)。
最佳答案
注意:已经有类似的问答here .
您需要在 RelativeLayout
中有一个小部件,您可以将其用作定位 TextView
的引用。水平居中的空 Space
小部件 (0dp x 0dp) 对此效果很好。
<RelativeLayout>
<!-- Empty space widget (0x0 dp) centered horizontally -->
<Space
android:id="@+id/spacer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"
android:visibility="invisible"/>
<!-- TextView to left of the spacer-->
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/spacer"
android:text="awesome text!"/>
</RelativeLayout>
关于android - 如何将 TextView 的结束边缘定位到布局的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34534291/