我正在使用xml布局在安卓中给图形添加彩色边框。可以只对三条边(左、上、下)添加边框而右边不添加吗?
我正在使用xml布局在安卓中给图形添加彩色边框。可以只对三条边(左、上、下)添加边框而右边不添加吗?
您可以通过使用层列表(layerlist)并调整填充(padding)来实现这一目标。您将需要3个元素:
1:border.xml形状,它只是您边框颜色的实心形状:border.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ff0000"/>
</shape>
2: "内部"图形,即您希望边框出现的图形:inner.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00ff00"/>
</shape>
3:一层列表,将这两个东西叠放在一起。通过在内部形状上设置填充来创建边框:layerlist.xml
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/border"/>
<item android:drawable="@drawable/inner"
android:top="3dp" android:right="0dp" android:bottom="3dp"
android:left="3dp" />
</layer-list>
使用@Mopper的答案,你也可以这样喜欢。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#ff0000" />
</shape>
</item>
<item
android:bottom="3dp"
android:left="3dp"
android:right="0dp"
android:top="3dp">
<shape>
<solid android:color="#00ff00" />
</shape>
</item>
</layer-list>
以下是我的解决方案,如果您想要透明或半透明的背景颜色,它将起作用。
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="-1dp"
>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:color="#c45b5b5b"
android:width="1dp"/>
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="0dp"
android:topRightRadius="5dp"
/>
</shape>
</inset>
</item>
<item
android:top="1dp"
android:right="1dp"
android:bottom="1dp"
android:left="0dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/infoBox_Background"/>
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="0dp"
android:topRightRadius="5dp"
/>
</shape>
</item>
</layer-list>
另一种方法是使用9-Patch png文件作为您的背景。您可以通过获取普通png文件,然后使用this等工具来定义:
生成9-patch png文件之后,将其添加到可绘制项(为各种分辨率定义文件)。然后将可绘制项设置为控件背景即可。
android:background="@drawable/my9_patch_file_name"