Android: a stroke in the form creates a border for the stroke width

I created a rectangular shape to use as the background of a list item. My problem is that the stroke does not follow the border of the view, but allows a border +/- the width of the stroke.

Here is the xml of my form:

<?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> 

And here is the xml of my style:

 <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> 

And complete 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> 
+13
source share
4 answers

The problem is solved using solid colors!

+3
source

Surround your shape with <item>...</item> tags and set the top , bottom , left and right <item> attributes with the values ​​of the fields you want . Then wrap the entire item <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> 

Also, if you need padding (instead of margin ), see Ahmad Agazad to answer below.

+22
source

You can use a transparent indent instead of a field

 <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape"> <stroke android:width="8dp" android:color="#0000000" /> <padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp" /> <solid android:color="@color/white" /> </shape> 
+6
source

I had the same problem and solved it like this:

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="@color/background" /> <corners android:radius="2dp"/> <stroke android:width="4dp" android:color="@android:color/black" /> </shape> </item> <item> <shape android:shape="rectangle"> <corners android:radius="2dp"/> <solid android:color="@android:color/transparent" /> <stroke android:width="2dp" android:color="@color/background" /> </shape> </item> </layer-list> 

It gives two graphics with two strokes. At first, the pushed ones have a stroke with a width (width + edge of the desired stroke). The second ejected ones have only a visible stroke with the width of the desired field.

+3
source

All Articles