首页 文章

有没有一种简单的方法可以在Android视图的顶部和底部添加边框?

提问于
浏览
356

我有一个TextView,我想沿其顶部和底部边框添加黑色边框 . 我尝试将 android:drawableTopandroid: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" />

有没有办法轻松地在Android中的视图(特别是TextView)中添加顶部和底部边框?

22 回答

  • 0

    使用 InsetDrawable 添加边框以插入边框的最简单方法,仅限鞋面边框:

    <?xml version="1.0" encoding="utf-8"?>
    <inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:insetBottom="-2dp"
        android:insetLeft="-2dp"
        android:insetRight="-2dp">
        <shape android:shape="rectangle">
    
            <solid android:color="@color/light_gray" />
            <stroke
                android:width=".5dp"
                android:color="@color/dark_gray" />
        </shape>
    </inset>
    
  • 1

    这是实现它的一种方法 .

    <?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="@color/grey_coaching_text" />
                </shape>
            </item>
    
            <item
                android:bottom="1dp"
                android:top="1dp">
                <shape android:shape="rectangle">
                    <solid android:color="@color/white" />
                </shape>
            </item>
        </layer-list>
    

    第一项为中风,第二项为实心背景 . 隐藏左右边界 .

  • 3
    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#72cdf4"
        android:text=" aa" />
    

    只需在要添加边框的文本下方添加此TextView

  • 1

    只是为了强制执行@phreakhead ´suser1051892 ´s答案, <item android:bottom|android:left|android:right|android:top> 如果为负,则必须大于 <stroke android:width> . 如果没有,项目的绘画将与笔画混合,你可能会认为这些值不起作用 .

  • 0

    在android 2.2中你可以做到以下几点 .

    创建一个xml drawable,例如/res/drawable/textlines.xml,并将其指定为TextView的background属性 .

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

    /res/drawable/textlines.xml

    <?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的纯色以第二个形状描边颜色复制 .

  • 252

    我使用了一个技巧,以便边框显示在容器外面 . 使用此技巧只会绘制一条线,因此将显示底层视图的背景 .

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
            android:bottom="1dp"
            android:left="-2dp"
            android:right="-2dp"
            android:top="-2dp">
            <shape android:shape="rectangle" >
                <stroke
                    android:width="1dp"
                    android:color="#FF000000" />
    
                <solid android:color="#00FFFFFF" />
    
                <padding android:left="10dp"
                    android:right="10dp"
                    android:top="10dp"
                    android:bottom="10dp" />
            </shape>
        </item>
    
    </layer-list>
    
  • 72

    选项1:形状可绘制

    如果您想在布局或视图周围设置边框,则可以设置背景,这是最简单的选项 . 在 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>
    

    如果您不想填充,可以删除 solid . 在布局/视图上设置 background="@drawable/your_shape_drawable" .

    选项2:背景视图

    这里's a little trick I' ve用于 RelativeLayout . 基本上你想要给出一个边框的视图下面有一个黑色方块,然后给那个视图一些填充(不是边距!),所以黑色方块在边缘显示 .

    显然,如果视图没有任何透明区域,这只能正常工作 . 如果确实如此,我建议您编写一个自定义 BorderView ,它只绘制边框 - 它应该只有几十行代码 .

    <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 中有一个背景,它就不起作用,因为有一个错误阻止你用 View 填充 RelativeLayout . 在那种情况下,我会推荐 Shape drawable .

    选项3:9补丁

    最后一个选择是使用像这样的9-patch drawable:

    您可以在任何可以设置 android:background="@drawable/..." 的视图上使用它 . 是的它确实需要6x6 - 我尝试了5x5并且它不起作用 .

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

    选项4:额外的观点

    如果您只想在视图的上方和下方设置边框,我忘了提及这个非常简单的选项 . 你可以将你的视图放在一个垂直的 LinearLayout (如果它还没有),然后在它上面和下面添加空的 View ,如下所示:

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

    要仅在底部添加 1dp 白色边框并具有透明背景,您可以使用以下内容,这比此处的大多数答案更简单 .

    对于 TextView 或其他视图添加:

    android:background="@drawable/borderbottom"
    

    并在 drawable 目录中添加以下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"

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

    可能不需要 solid 元素 . 这将取决于您的设计(感谢V. Kalyuzhnyu) .

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

  • 7

    所以我想做一些稍微不同的事情:仅在底部的边框,以模拟ListView分隔符 . 我修改了Piet Delport的答案并得到了这个:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
       <item>
          <shape 
            android:shape="rectangle">
                <solid android:color="@color/background_trans_light" />    
    
            </shape>
       </item>
    
        <!-- this mess is what we have to do to get a bottom border only. -->
       <item android:top="-2dp"
             android:left="-2dp"
             android:right="-2dp"
             android:bottom="1px"> 
          <shape 
            android:shape="rectangle">    
                <stroke android:width="1dp" android:color="@color/background_trans_mid" />
                <solid android:color="@null" />
            </shape>
       </item>
    
    </layer-list>
    

    注意使用px代替dp来获得精确的1像素分频器(某些手机DPI会使1dp线消失) .

  • 4

    目前接受的答案不起作用 . 由于抗锯齿,它会在视图的左侧和右侧创建细垂直边框 .

    这个版本很完美 . 它还允许您独立设置边框宽度,如果需要,还可以在左/右侧添加边框 . 唯一的缺点是它不支持透明度 .

    使用下面的代码创建一个名为 /res/drawable/top_bottom_borders.xml 的xml drawable,并将其指定为TextView的背景属性 .

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <solid android:color="#DDDD00" /> <!-- border color -->
            </shape>
        </item>
    
        <item
            android:bottom="1dp" 
            android:top="1dp">   <!-- adjust borders width here -->
            <shape android:shape="rectangle">
                <solid android:color="#FFFFFF" />  <!-- background color -->
            </shape>
        </item>
    </layer-list>
    

    Tested on Android KitKat through Marshmallow

  • 0

    正如@Nic Hubbard所说,有一种非常简单的方法来添加边界线 .

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#000000" >
    </View>
    

    您可以将高度和背景颜色更改为您想要的任何颜色 .

  • 8

    您还可以将视图包装在FrameLayout中,然后将框架的背景颜色和填充设置为您想要的内容;但是,默认情况下,textview具有“透明”背景,因此您还需要更改textview的背景颜色 .

  • -1

    我的答案是基于@Emile版本,但我使用透明色而不是实色 .
    此示例将绘制一个2dp底部边框 .

    <?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="2dp"
                         android:color="#50C0E9" />
                <solid android:color="@android:color/transparent" />
            </shape>
        </item>
        <item  android:bottom="2dp" >
            <shape android:shape="rectangle" >
                <stroke  android:width="2dp"
                         android:color="@color/bgcolor" />
                <solid android:color="@android:color/transparent" />
            </shape>
        </item>
    </layer-list>
    

    @color/bgcolor 是您用边框绘制视图时背景的颜色 .

    如果要更改边框的位置,请使用以下方法之一更改偏移:

    android:bottom="2dp"
    android:top="2dp"
    android:right="2dp"
    android:left="2dp"
    

    或将它们组合成2个或更多边框:

    android:bottom="2dp" android:top="2dp"
    
  • 33

    为什么不创建一个背景色的1dp高视图?然后它可以轻松放置在您想要的位置 .

  • -1

    首先制作一个包含如下所示内容的xml文件,并将其命名为border.xml,并将其放在res目录中的layout文件夹中

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <stroke android:width="1dp" android:color="#0000" />
        <padding android:left="0dp" android:top="1dp" android:right="0dp"
            android:bottom="1dp" />
    </shape>
    

    之后在代码内部使用

    TextView tv = (TextView)findElementById(R.id.yourTextView);
    tv.setBackgroundResource(R.layout.border);
    

    这将在TextView的顶部和底部形成一条黑线 .

  • 389

    要改变这个:

    <TextView
        android:text="My text"
        android:background="@drawable/top_bottom_border"/>
    

    我更喜欢“drawable / top_bottom_border.xml”中的这种方法:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape>
                <gradient
                    android:angle="270"
                    android:startColor="#000"
                    android:centerColor="@android:color/transparent"
                    android:centerX="0.01" />
            </shape>
        </item>
        <item>
            <shape>
                <gradient
                    android:angle="90"
                    android:startColor="#000"
                    android:centerColor="@android:color/transparent"
                    android:centerX="0.01" />
            </shape>
        </item>
    </layer-list>
    

    这只会产生边框,而不是在背景有颜色时会出现的矩形 .

  • 94

    只是将我的解决方案添加到列表中..

    我想要一个半透明的底部边框,它延伸超过原始形状(因此半透明边框位于父矩形之外) .

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
      <item>
        <shape android:shape="rectangle" >      
          <solid android:color="#33000000" /> <!-- Border colour -->
        </shape>
      </item>
      <item  android:bottom="2dp" >
        <shape android:shape="rectangle" >     
          <solid android:color="#164586" />
        </shape>
      </item>
    </layer-list>
    

    哪个给了我;

    enter image description here

  • 7

    记下下面的代码

    <View
        android:layout_width="wrap_content"
        android:layout_height="2dip"
        android:layout_below="@+id/topics_text"
        android:layout_marginTop="7dp"
        android:layout_margin="10dp"
        android:background="#ffffff" />
    
  • 35

    尝试使用线性布局包装图像,并将其背景设置为文本周围所需的边框颜色 . 然后将textview上的填充设置为边框所需的厚度 .

  • 0

    您还可以使用9路径来完成工作 . 创建它,使彩色像素的高度不会增加,而只会增加透明像素 .

  • 4
    // Just simply add border around the image view or view
    
    <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="90dp"
                    android:layout_height="70dp"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="10dp"
                    android:layout_toLeftOf="@+id/imageView1"
                    android:background="@android:color/white"
                    android:padding="5dip" />
    
    // After that dynamically put color into your view or image view object
    
    objView.setBackgroundColor(Color.GREEN);
    
    //VinodJ/Abhishek
    
  • 3
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    
    <solid android:color="@color/light_grey1" />
    <stroke
        android:width="1dip"
        android:color="@color/light_grey1" />
    
    <corners
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="0dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />
    
        </shape>
    

相关问题