为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕妹3146593 2019-07-12 16:12:55
有没有一种简单的方法可以将边框添加到Android视图的顶部和底部?我有一个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" />有没有一种方法可以方便地向Android中的View(特别是TextView)添加顶部和底部边框?
查看完整描述

3 回答

?
森林海

TA贡献2011条经验 获得超2个赞

选项1:形状可绘制

如果您希望在布局或视图周围设置一个边框,则这是最简单的选项。在drawable如下所示的文件夹:

<?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:背景视图

这是我用过的一个小技巧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,因为有一个bug阻止您填充RelativeLayout带着View..在这种情况下,我会推荐Shape可拖。

备选方案3:9-补丁

最后一种选择是使用像这样的9补丁可绘制:

您可以在可以设置的任何视图上使用它。android:background="@drawable/..."..是的,它确实需要6x6-我尝试了5x5,但它没有工作。

这种方法的缺点是您不能很容易地更改颜色,但是如果您想要花哨的边框(例如,在顶部和底部只有一个边框,如这个问题),那么您可能无法使用Shape可拖的,这不是很强大。

备选方案4:额外视图

我忘了提到这个非常简单的选项,如果您只想在视图上方和下面设置边框。您可以将视图放置在垂直位置。LinearLayout(如果还没有),然后添加空View上面和下面都是这样的:

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



查看完整回答
反对 回复 2019-07-12
  • 3 回答
  • 0 关注
  • 300 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信