【问题标题】:How to hide view behind the view如何隐藏视图后面的视图
【发布时间】:2017-12-06 07:33:15
【问题描述】:

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

提前致谢!

像这样

【问题讨论】:

  • 你能把图片粗略画一下并附上图片吗
  • 你能回答吗?
  • 我没有得到确切的问题,如果你可以请分享粗略的图像
  • 可能更容易拥有 3 个视图。 ImageView TextView ImageView
  • 你能回答我能更好地理解吗:)

标签: java android android-layout textview android-xml


【解决方案1】:

试试这个

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

输出

【讨论】:

  • 所以你用布局来隐藏背景线?
  • @blackHawk 是的,或者您也可以使用 RelativeLayout 查看第一个解决方案
  • @blackHawk 我已经完成了两种方式,首先使用相对布局,其次使用线性布局
  • 在我的例子中,根布局是相对布局,我将 textview 包装在线性布局中并放在线上让我们看看结果....编译
  • @blackHawk 如有任何疑问,请告诉我
【解决方案2】:

您可以使用 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>

【讨论】:

    【解决方案3】:

    我认为有两种选择:

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

    【讨论】:

      【解决方案4】:

      你可以试试下面的代码

      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>
      

      这是输出:

      【讨论】:

        猜你喜欢
        • 2016-07-03
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多