如何隐藏视图后面的视图

How to hide view behind the view

我将 textview 放在一条线上,因为 textview 具有浅色背景 textview 后面的线是可见的,我想隐藏后面的那部分线textview,我该怎么做

提前致谢!

像这样

您可以使用 RelativeLayout 以多种方式完成此操作。

 <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <View
        android:layout_width="match_parent"
        android:layout_centerVertical="true"
        android:background="#000000"
        android:layout_height="2dp"/>
    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:background="#FFFFFF"
        android:paddingLeft="10dp"
        android:paddingRight="10sp"
        android:layout_centerInParent="true"
        android:text="Some Demo text"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
         />
</RelativeLayout>

其他方式如.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <View
        android:layout_width="match_parent"
        android:layout_centerVertical="true"
        android:background="#000000"
        android:layout_toLeftOf="@+id/txt"
        android:layout_height="2dp"/>
    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10sp"
        android:layout_centerInParent="true"
        android:text="Some Demo text"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
         />

    <View
        android:layout_width="match_parent"
        android:layout_centerVertical="true"
        android:background="#000000"
        android:layout_toRightOf="@+id/txt"
        android:layout_height="2dp"/>
</RelativeLayout>

试试这个

<?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="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_centerInParent="true"
                android:background="@color/colorAccent" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:background="@color/colorPrimary"
                android:gravity="center"
                android:text="Nilesh" />
        </RelativeLayout>
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:gravity="center"
        android:orientation="horizontal">

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_centerInParent="true"
            android:layout_weight="1"
            android:background="@color/colorAccent" />


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#FFFFFF"
            android:text="NILu" />


        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_centerInParent="true"
            android:layout_weight="1"
            android:background="@color/colorAccent" />


    </LinearLayout>

</LinearLayout>

OUTPUT

我认为有两种选择:

  1. 您并排使用三个视图来获得行 - 文本 - 行
  2. 您在 FrameLayout 中使用了两个视图:第一个是直线的视图,第二个是 TextView,绘制在直线上方。确保 TextView 具有非透明背景并且其 layout_widthwrap_content。根据您想要的结果,您可能希望向 TextView 添加一些水平填充,以便该行不会直接接触文本。

您可以像下面的代码一样尝试

shape_line.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line"
    >
    <solid android:color="#FFFFFF">
    </solid>
    <stroke
        android:width="1dp"
        android:color="#000000" />
    <size android:height="2dp" />
</shape>

将其添加到您的可绘制文件夹中。 并在您的布局中添加以下布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/shape_bg_line"
    tools:ignore="MissingConstraints">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#FFFFFF"
        android:padding="5dp"
        android:text="Hello" />


</RelativeLayout>

这是输出: