One way Android move?

Is it possible to create an Android shape object with a stroke only on certain sides?

For example, I have:

<stroke android:width="3dip" android:color="#000000" android:dashWidth="10dip" android:dashGap="6dip" /> 

What looks like this CSS:

 border: 3px dashed black; 

How to set the move on only one side? Here is how I would do it in CSS:

 border-left: 3px dashed black; 

How do you do this in Android XML?

+87
android android-drawable android-shape
Mar 11
source share
7 answers

I reached a good solution with this:

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- This is the line --> <item android:top="-1dp" android:right="-1dp" android:left="-1dp"> <shape> <solid android:color="@android:color/transparent" /> <stroke android:width="1dp" android:color="#ffffff" /> </shape> </item> </layer-list> 

This works well if you need a transparent background , but still the color of the open stroke (in my case, I only need the bottom line). If you need a background color, you can add a solid shape color, as in Mary’s answer.

EDIT 1

Sometimes for high-density devices, the use of low immersion values ​​can result in very thin or invisible strokes or distances. This can happen to you when setting ListView delimiters.

The simplest workaround is to use 1px distance instead of 1dp. This will make the line always visible at all densities. The best solution would be to create measurement resources for each density to get the best size for each device.

Edit 2

Fun, but I tried to use this after 6 years, and I can't get a good result on Lollipop devices.

Perhaps the current solution is to use the 9 patch. Android should have easily solved this problem after this time.

+118
Dec 14 '11 at 9:01
source share

I solved this using the list layer, combining two shapes, one of which with a height of 1dp, located at the bottom

optionscreen_bottomrectangle.xml:

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- This is the line --> <item> <shape> <solid android:color="#535353" /> </shape> </item> <!-- This is the main color --> <item android:bottom="1dp"> <shape> <solid android:color="#252525" /> </shape> </item> </layer-list> 

Then in layout / main.xml

 <TextView android:id="@+id/bottom_rectangle" android:background="@drawable/optionscreen_bottomrectangle" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_below="@id/table_options" android:layout_above="@id/exit_bar"/> 

Fills the gap between table_options and exit_bar with the background and just before exit_bar prints the 1dp line. It helped me, I hope it helps someone else.

The answer has been edited to place the layers in the correct order. thanks Alex!

+39
Mar 30 '10 at 16:04
source share

I know this question was posted a long time ago, so the answer will not be used by the person who posted this question, but it can still help others.

  <?xml version="1.0" encoding="UTF-8"?> <!-- inset is used to remove border from top, it can remove border from any other side--> <inset xmlns:android="http://schemas.android.com/apk/res/android" android:insetTop="-2dp" > <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rectangle"> <stroke android:width="1dp" android:color="#b7b7b7" /> <corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp"/> <solid android:color="#454444"/> </shape> </inset> 

Use the inset tag and give a negative value for the side border you want to remove.

Possible values:

android:insetTop="-1dp" android:insetBottom="-1dp" android:insetLeft="-1dp" android:insetRight="-1dp"

+36
Aug 21 '15 at 17:05
source share

Another, answer the other answers using a space instead. This little snipplet makes a border on top and bottom.

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- This is the line --> <item> <shape> <padding android:left="0dp" android:top="1dp" android:right="0dp" android:bottom="1dp"/> <solid android:color="#898989" /> </shape> </item> <!-- This is the main color --> <item> <shape> <solid android:color="#ffffff" /> </shape> </item> </layer-list> 
+30
Apr 11 '13 at 0:14
source share
Answer to

@Maragues is the opposite, since draw-draw for a layer-layer draws from top to bottom (this means that the last item in the list is drawn from above):

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- This is the line --> <item> <shape> <solid android:color="#535353" /> </shape> </item> <!-- This is the main color --> <item android:bottom="1dp"> <shape> <solid android:color="#252525" /> </shape> </item> </layer-list> 

This will effectively fill the form with the color of the line, and then draw a background color on top of it, leaving the last 1dp transparent for the color of the line to be displayed.

+26
Sep 12 '11 at 21:40
source share
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape android:shape="rectangle" > <stroke android:width="2dp" android:color="#BBBBBB" /> <solid android:color="@android:color/transparent" /> </shape> </item> <item android:bottom="2dp" > <shape android:shape="rectangle" > <stroke android:width="2dp" android:color="@color/main_background_color" /> <solid android:color="@android:color/transparent" /> </shape> </item> 

+2
Nov 07 '13 at 11:15
source share

I used the following code.

 <?xml version="1.0" encoding="UTF-8"?> <!-- inset is used to remove border from top, it can remove border from any other side--> <inset xmlns:android="http://schemas.android.com/apk/res/android" android:insetTop="-2dp" android:insetLeft="-2dp" android:insetRight="-2dp" > <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rectangle"> <stroke android:width="1dp" android:color="@color/colorPrimary" /> <solid android:color="#0000"/> <padding android:left="2dp" android:top="2dp" android:bottom="2dp" android:right="2dp"/> </shape> </inset> 
+2
Mar 25 '16 at 6:45
source share



All Articles