Android TextView两侧的水平线
无法找到一种方法,使居中的文本视图两侧都有垂直居中的水平线,并延伸到屏幕的左端和右端。就像下面这张照片Android TextView两侧的水平线,android,android-layout,Android,Android Layout,无法找到一种方法,使居中的文本视图两侧都有垂直居中的水平线,并延伸到屏幕的左端和右端。就像下面这张照片 任何见解都会有帮助 我就是这样做的: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layou
任何见解都会有帮助 我就是这样做的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:padding="5dp" >
<View
android:layout_width="90dp"
android:layout_height="1dp"
android:layout_marginTop="12dp"
android:background="#999999" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="DESCRIPTION"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginTop="12dp"
android:background="#999999" />
</LinearLayout>
</LinearLayout>
我知道这不是一个完美的答案,但可能会给你一个想法
下面是它的外观:
终于想出了这个解决方案,它完全满足了需要
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/light_grey"
android:layout_gravity="center_vertical"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add_heading_description"
android:textSize="@dimen/add_headings"
android:textColor="@color/dark_grey"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_gravity="center"
android:background="@color/white"
/>
</FrameLayout>
我就是这样做的,看起来和我想要的一模一样
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginRight="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="8dp">
<View
android:layout_gravity="center_vertical"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"
android:background="#FFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="or"
android:textColor="#FFF"
android:paddingLeft="16dp"
android:paddingRight="16dp" />
<View
android:layout_gravity="center_vertical"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"
android:background="#FFF" />
</LinearLayout>
我对接受的答案有疑问,因此分享我的方法:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="0dp"
android:orientation="horizontal">
<View
android:layout_width="40dp"
android:layout_height="1dp"
android:background="@color/secondary_text"
android:layout_gravity="center_vertical"
android:layout_weight="1"/>
<Button
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:text="@string/Or"
android:clickable="false"
android:textColor="@color/secondary_text" />
<View
android:layout_width="40dp"
android:layout_height="1dp"
android:background="@color/secondary_text"
android:layout_gravity="center_vertical"
android:layout_weight="1"/>
</LinearLayout>
文本视图不会居中,是吗?您需要随着每次文本/字体大小的更改而更改90dp宽度,使其看起来像居中一样?没错,正如我提到的,这个答案并不完美。。只是想达到这些目标。好的,我只是想看看我是否理解正确。如果能找到一种方法使文本视图真正居中,那就太好了……是的,我认为我们需要对布局进行一些调整。如果你想在一个屏幕上显示类似的内容,这将是一个不错的解决方案。想象一下,如果你需要5+个屏幕。然后呢?好的建议是:自定义布局并使用
或使用稍微修改的onDraw(Canvas Canvas)
方法创建CustomTextView
。我自己选择了#2。干杯