I am using a stroke to make a colored border to a shape in xml layout in android .
我正在使用笔划在android中的xml布局中为彩色边框制作一个形状。
Can I make the stroke for only 3 edges ( left,top,bottom) the right NO ?
我只能将3个边缘(左边,顶部,底部)的笔划设为正确的NO吗?
4 个解决方案
#1
48
You can achieve this by using a layerlist and messing with the padding. You'll need 3 elements:
您可以通过使用图层列表并弄乱填充来实现此目的。你需要3个元素:
1: A border.xml shape, which is just a solid shape in the color of your border: border.xml
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: The 'inner' shape, the shape where you want the border to appear around: inner.xml
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: A layer list, which will put these 2 on top of eachother. You create the border by setting the padding on the inner shape: layerlist.xml
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>
#2
1
Another approach would be to use a 9-Patch png file for your background. You do this by taking a normal png file and then using a tool like this to define:
另一种方法是使用9-Patch png文件作为背景。你可以通过一个普通的png文件,然后使用这样的工具来定义:
- The area where your content goes in the png.
- The area of your png that stretches as the underlying control grows in either width or height.
您的内容在png中的区域。
当底层控件在宽度或高度上增长时,png的区域会拉伸。
Once you generate the 9-patch png file, add it to your drawables (define files for various resolutions). Then just set the drawable as your control background.
生成9-patch png文件后,将其添加到drawables(定义各种分辨率的文件)。然后将drawable设置为您的控件背景。
android:background="@drawable/my9_patch_file_name"
#3
1
Here's my solution, which works if you want transparent or semi-transparent background colors.
这是我的解决方案,如果您想要透明或半透明背景颜色,则可以使用。
<?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>
#4
1
Using @Mopper answer, you can like that too.
使用@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>
#1
48
You can achieve this by using a layerlist and messing with the padding. You'll need 3 elements:
您可以通过使用图层列表并弄乱填充来实现此目的。你需要3个元素:
1: A border.xml shape, which is just a solid shape in the color of your border: border.xml
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: The 'inner' shape, the shape where you want the border to appear around: inner.xml
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: A layer list, which will put these 2 on top of eachother. You create the border by setting the padding on the inner shape: layerlist.xml
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>
#2
1
Another approach would be to use a 9-Patch png file for your background. You do this by taking a normal png file and then using a tool like this to define:
另一种方法是使用9-Patch png文件作为背景。你可以通过一个普通的png文件,然后使用这样的工具来定义:
- The area where your content goes in the png.
- The area of your png that stretches as the underlying control grows in either width or height.
您的内容在png中的区域。
当底层控件在宽度或高度上增长时,png的区域会拉伸。
Once you generate the 9-patch png file, add it to your drawables (define files for various resolutions). Then just set the drawable as your control background.
生成9-patch png文件后,将其添加到drawables(定义各种分辨率的文件)。然后将drawable设置为您的控件背景。
android:background="@drawable/my9_patch_file_name"
#3
1
Here's my solution, which works if you want transparent or semi-transparent background colors.
这是我的解决方案,如果您想要透明或半透明背景颜色,则可以使用。
<?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>
#4
1
Using @Mopper answer, you can like that too.
使用@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>