android - 将不同颜色的边框添加到android View 的顶部和底部边缘的方法

标签 android xml android-layout user-interface

我有一个 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="#000000"/>
    </shape>
</item>

<item
    android:top="1dp" android:bottom="1dp">
    <shape android:shape="rectangle">
        <solid android:color="#ffffff"/>
    </shape>
</item>

但是如果我们需要不同颜色的边,需要做什么呢?

最佳答案

你已经很接近你想要的了,你需要做的是在你的默认项目下面添加另一个项目。这两项是您的顶部/底部边框。通过向两者添加 bottom/top 1dp,您可以显示两种颜色。

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

<item
    android:top="1dp">
    <shape android:shape="rectangle">
        <solid android:color="#000000"/>
    </shape>
</item>

<item
    android:top="1dp" android:bottom="1dp">
    <shape android:shape="rectangle">
        <solid android:color="#ffffff"/>
    </shape>
</item>
</layer-list>

关于android - 将不同颜色的边框添加到android View 的顶部和底部边缘的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35705896/

相关文章:

java - Google Docs List API PDF 文件下载不工作 (Android)

xml - XSLT 将外部图形装入 block 中

android - RelativeLayout 中子级的 match_parent 属性

android - 如何在 ImageView 顶部显示指示器气泡?

android - 如何创建这样的 ListView? (交通柜台)

java - 设置更改后,Google Maps Android API v2 不显示 map

android - 使用 Android Camera2 API 进行人脸检测和画圈

android - 如何更改android中星星侧面 View 的颜色?

android - 为 ExpandableListView 设置自定义指示器不起作用

java - 使用 Apache XMLBeans 对字符串中的 XML 实体进行编码