Android:在形状中描边创建描边宽度的边距 [英] Android : stroke in a shape create a margin of the stroke width
本文介绍了Android:在形状中描边创建描边宽度的边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个矩形形状,以便将其用作列表项背景.我的问题是笔触不跟随视图边框,而是让笔划宽度的+/-留有边距.
I created a rectangle shape in order to use it as list item background. My problem is the stroke does not follow the view border but let a margin of +/- the stroke width.
这是我形状的xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="@color/deminoir" />
<stroke
android:width="4dp"
android:color="@color/deminoir" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
这是我风格的xml:
<style name="champ">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:orientation">vertical</item>
<item name="android:background">@drawable/bordurechamp</item>
</style>
最后确定我的列表项视图的xml:
And finaly the xml of my list item view :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/champ" >
<!-- titre -->
<TextView
android:id="@+id/titre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/deminoir"
android:padding="5dip"
android:text="titre"
android:textAppearance="@android:style/TextAppearance.Large" />
<!-- Contenu -->
<TextView
android:id="@+id/valeur"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="5dip"
android:text="valeur"
android:textAppearance="@android:style/TextAppearance.Medium"
/>
</LinearLayout>
推荐答案
使用< item> ...</item>
标签环绕您的 shape
在< item>
的 top
, bottom
, left
和 right
属性上 所需的边距值 .然后用< layer-list>
包装整个 item
.见下文:
Surround your shape
with <item>...</item>
tags and set up top
, bottom
, left
and right
attributes of <item>
with margin values you want. Then wrap whole item
with a <layer-list>
. See below:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="{L-Margin}dp"
android:right="{R-Margin}dp"
android:bottom="{B-Margin}dp"
android:top="{T-Margin}dp">
<!-- Insert your shape here: -->
<shape android:shape="...">
...
</shape>
</item>
</layer-list>
如果您要 padding
(而不是 margin
),请参见下面的艾哈迈德·阿加扎德(Ahmad Aghazadeh)答案.
这篇关于Android:在形状中描边创建描边宽度的边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文