我有一个TextView,我想添加一个黑色边界沿其顶部和底部边界。我尝试添加android:drawableTop和android:drawableBottom到TextView,但这只会导致整个视图变成黑色。

<TextView
    android:background="@android:color/green"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawableTop="@android:color/black"
    android:drawableBottom="@android:color/black"
    android:text="la la la" />

有没有一种方法可以轻松地添加一个顶部和底部边界的视图(特别是TextView)在Android?


当前回答

为什么不直接用背景色创建一个1dp高视图呢?然后它可以很容易地放在你想要的地方。

其他回答

为了只在底部添加1dp白色边框,并且有一个透明的背景,你可以使用下面的方法,这比这里的大多数答案都简单。

对于TextView或其他视图添加:

android:background="@drawable/borderbottom"

在可绘制目录中添加以下XML,称为borderbottom.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="-2dp" android:left="-2dp" android:right="-2dp">
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#ffffffff" />
            <solid android:color="#00000000" />
        </shape>
    </item>
</layer-list>

如果你想在顶部设置边框,将android:top="-2dp"改为android:bottom="-2dp"

颜色不需要是白色的,背景也不需要是透明的。

固体元素可能不是必需的。这取决于你的设计(感谢V. Kalyuzhnyu)。

基本上,这个XML将使用矩形形状创建边框,但随后将顶部、右侧和左侧推到形状的渲染区域之外。这样只留下底部边界可见。

选项1:Shape Drawable

这是最简单的选项,如果你想在布局或视图周围设置一个边框,你可以在其中设置背景。在可绘制文件夹中创建一个XML文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#8fff93" />

    <stroke
        android:width="1px"
        android:color="#000" />

</shape>

如果你不想填充,你可以去掉固体。set background="@drawable/your_shape_drawable"在你的布局/视图。

选项2:背景视图

这是我在RelativeLayout中使用的一个小技巧。基本上,你在视图下有一个黑色的正方形,你想给它一个边框,然后给这个视图一些填充(不是边距!),这样黑色的正方形就会在边缘显示出来。

显然,这只有在视图没有任何透明区域时才能正常工作。如果是这样的话,我建议你写一个只画边界的自定义边界视图——它应该只有几十行代码。

<View
    android:id="@+id/border"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/image"
    android:layout_alignLeft="@+id/image"
    android:layout_alignRight="@+id/image"
    android:layout_alignTop="@+id/main_image"
    android:background="#000" />

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_...
    android:padding="1px"
    android:src="@drawable/..." />

如果你想知道,它的工作与adjustViewBounds=true。然而,如果你想在整个RelativeLayout中有一个背景,它就不起作用了,因为有一个错误会阻止你用视图填充RelativeLayout。在这种情况下,我建议使用Shape drawable。

选项3:9-patch

最后一个选项是使用一个9补丁的可绘制图,如下图所示:

你可以在任何视图上使用它,你可以设置android:background="@drawable/…"是的,它确实需要6x6 -我试过5x5,但它不起作用。

这种方法的缺点是你不能很容易地改变颜色,但如果你想要花哨的边框(例如,只有顶部和底部的边框,就像这个问题一样),那么你可能无法用Shape drawable来做这些,这不是很强大。

选项4:额外的视图

我忘记提到这个非常简单的选项,如果你只想让边界在视图的上方和下方。你可以把你的视图放在一个垂直的线性布局中(如果它还没有),然后在它的上面和下面添加空视图,就像这样:

<View android:background="#000" android:layout_width="match_parent" android:layout_height="1px"/>

只需在视图的顶部和底部添加视图

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/your_color"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="@+id/textView"
        app:layout_constraintStart_toStartOf="@+id/textView" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:gravity="center"
        android:text="Testing"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/your_color"
        app:layout_constraintEnd_toEndOf="@+id/textView"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

</android.support.constraint.ConstraintLayout>

在android 2.2中,您可以执行以下操作。

创建一个xml可绘制对象,如/res/drawable/textlines.xml,并将其指定为TextView的背景属性。

<TextView
android:text="My text with lines above and below"
android:background="@drawable/textlines"
/>

xml / res / drawable - textlines。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="#FF000000" />
            <solid android:color="#FFDDDDDD" />

        </shape>
   </item>

   <item android:top="1dp" android:bottom="1dp"> 
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="#FFDDDDDD" />
            <solid android:color="#00000000" />
        </shape>
   </item>

</layer-list>

这样做的缺点是你必须指定一个不透明的背景颜色,因为透明度是行不通的。(至少我是这么认为的,但我错了)。在上面的例子中,你可以看到第一个形状#FFdddddd的纯色被复制到了第二个形状的描边颜色中。

您可以通过以下代码片段来实现这一点-

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--Minus (-) how much dp you gave in the stroke width from left right-->
    <item android:left="-10dp" android:right="-10dp">
        <shape
            android:shape="rectangle">
            <stroke android:width="10dp" android:color="@android:color/holo_red_dark" />
           <!--This is the main background -->
            <solid android:color="#FFDDDDDD" />
        </shape>
    </item>
</layer-list>

预览,