【发布时间】:2016-12-26 04:13:17
【问题描述】:
无法找到一种方法来让一个居中的 TextView 在两边都有垂直居中的水平线,拉伸到屏幕的左右端。如下图所示。
任何见解都会有所帮助!
【问题讨论】:
无法找到一种方法来让一个居中的 TextView 在两边都有垂直居中的水平线,拉伸到屏幕的左右端。如下图所示。
任何见解都会有所帮助!
【问题讨论】:
终于想出了这个完全符合要求的解决方案
<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: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>
【讨论】:
这就是我的做法:
<?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>
我知道这不是一个完美的答案,但可能会给你一个想法。
这是它的外观:
【讨论】:
我就是这样做的,看起来和我想要的完全一样。
<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>
【讨论】:
<include> 或创建CustomTextView 并稍微修改onDraw(Canvas canvas) 方法。我自己选择了#2。干杯!