1. Nesting a layer, using the content of the margin or padding to the background of the container to achieve the border effect.
In fact, this is very simple, very early in the time we also use table to do HTML page layout, we are using cellspacing to implement the table's border. Now we are using the same idea to achieve it.
Android implementation of the Border effect scheme a
Where padding produces white, it creates a border-like effect.
<linearlayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:padding= "2px" android:background= "#f00" >
<textview android:layout_width= "Fill_parent android:layout_height=" Wrap_ Content "android:text=" Hello world! "android:background=" #000 "></TextView>
</LinearLayout>
Of course, internal containers also need to have color, if you want to achieve the effect of internal transparency to the internal view of the background color and background color consistent, this is not very convenient place.
2. Use 9-patch (nine sudoku) background picture to achieve the border effect.
Make a 9-patch picture with a border, as the background of the view you want to have a border. You can also control which side has a border, which side has no border, this method is a better way, and there is no redundant view nesting. In addition, you can also achieve the effect of rounded corners.
Android implementation of the Border Effect Scheme II
If you don't know anything about 9-patch, please go. You need to make a PNG picture with a border, using the Draw9patch.bat tool under the SDK's Tools folder, you can easily generate 9-patch images. Tools are simple to use. The left and top lines are used to extend the content that is displayed repeatedly, and the right and bottom lines are used to control where the content is displayed. Here I want to thank the country first, we now have free access to the Android developer site.
3. Use the shape of the stroke to achieve the border effect.
Of course you can use the stroke in shape to achieve the border effect.
<?xml version= "1.0" encoding= "Utf-8"?> <shape xmlns:android=
"http://schemas.android.com/apk/res/" Android >
<stroke android:width= "1DP" android:color= "#f00"/>
</shape>
You can then refer to the shape as a background to achieve the border effect. For more information about shape, please go to the venue.
The final results show:
Of course there are many other workarounds, such as you can also add a view to set the width to 1DP, as a border. This will require you to be flexible in the actual development of the application.