【问题标题】:Horizontal lines on either sides of TextViewTextView 两边的水平线
【发布时间】:2016-12-26 04:13:17
【问题描述】:

无法找到一种方法来让一个居中的 TextView 在两边都有垂直居中的水平线,拉伸到屏幕的左右端。如下图所示。

任何见解都会有所帮助!

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    终于想出了这个完全符合要求的解决方案

        <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>
    

    【讨论】:

      【解决方案2】:

      我对接受的答案有疑问,所以分享我的方式:

       <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>
      

      【讨论】:

        【解决方案3】:

        这就是我的做法:

        <?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>
        

        我知道这不是一个完美的答案,但可能会给你一个想法。

        这是它的外观:

        Test Image http://imageshack.com/a/img854/9623/exly.jpg

        【讨论】:

        • 这不会使文本视图居中,是吗?您需要在每次更改文本/字体大小时更改 90dp 宽度以使其看起来像居中?
        • 没错,正如我所提到的,这个答案并不完美。只是实现这些线条。任何能让它变得更好的东西也会很好。
        • 好的,只是检查我是否理解正确。能找到一种方法让 textview 真正居中会很棒...
        • 是的,我认为我们需要对布局进行一些调整
        【解决方案4】:

        我就是这样做的,看起来和我想要的完全一样。

        <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>
        

        【讨论】:

        • 如果您想在一个屏幕上显示类似的内容,这将是一个不错的解决方案。想象一下,如果您需要在 5 个以上的屏幕中使用它。然后怎样呢?好的建议是:自定义布局并使用&lt;include&gt; 或创建CustomTextView 并稍微修改onDraw(Canvas canvas) 方法。我自己选择了#2。干杯!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-29
        • 2022-01-19
        • 2019-12-02
        • 2012-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多