【发布时间】:2014-08-05 08:24:08
【问题描述】:
我有这样的布局来显示一些标题:
但是当居中的文本很长时,视图不好。 这就是发生的事情:
如何获得这样的东西:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<View
android:background="#0000CC"
android:layout_width="fill_parent"
android:layout_height="4dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/headerTitle"
android:layout_margin="4dp"
android:visibility="invisible"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/headerTitle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:text="The quick brown fox jumps over the lazy dog"
android:textSize="20sp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:layout_centerInParent="true" />
<View
android:background="#CC0000"
android:layout_width="fill_parent"
android:layout_height="4dp"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/headerTitle"
android:layout_margin="4dp"
android:layout_centerVertical="true" />
感谢您的帮助
UPD:最接近的解决方案:
我对项目使用 LinearLayout + layout_width="0dp" + layout_weight=0.15/0.7/0.15。
【问题讨论】:
-
非常简单.. 两边都加内边距。
-
填充没有帮助。
-
将
android:layout_centerVertical="true"更改为android:layout_gravity="center_vertical"在您的<View ...标签中,并从<TextView ...标签中删除 android:layout_centerInParent="true"。不要忘记清除您的项目。 -
layout_gravity 也解决不了问题
标签: android android-layout android-relativelayout