Android rounded corner View implementation and compatibility with different versions, android rounded corner
When creating our own apps, we need to make our own views rounded to make the apps look better. After all, the mainstream also makes many things rounded, it looks similar to Apple and looks good.
It is easy to make a View into a rounded corner. You only need to create a custom Drawable.
We create a shape drawable under res/drawable. The Code is as follows:
[Html]View plaincopy
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Shape xmlns: android = "http://schemas.android.com/apk/res/android">
- <Corners
- Android: topLeftRadius = "5dp"
- Android: topRightRadius = "5dp"
- Android: bottomLeftRadius = "5dp"
- Android: bottomRightRadius = "5dp"/>
- <Stroke
- Android: width = "1dp"
- Android: color = "# FF5500"
- />
- </Shape>
In this example, corners refers to the rounded corner and stroke refers to the border color. For details about the shape, refer to the official documentation or search for it online.
There was no problem, but I was working on the problem that there were only rounded corners on the left and only rounded corners on the right, that is, before android3.0, android: bottomLeftRadius and android: bottomRightRadius is the opposite. That is to say, I set it to the left, but it becomes a diagonal display. Because I have not learned it well, I have not found a solution for a long time on the Internet, after knowing the android loading layout file method, you will know the solution, this is recorded here, is to create a file directory named drawable-v12 under our res, this file directory is accessed by Versions later than android3.0, so you only need to place the correct layout file in it.
The Code is as follows:
Write the following in android3.0 (rounded corners on the left ):
[Html]View plaincopy
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Shape xmlns: android = "http://schemas.android.com/apk/res/android">
- <Corners
- Android: topLeftRadius = "5dp"
- Android: bottomRightRadius = "5dp"/>
- <Stroke
- Android: width = "1dp"
- Android: color = "# FF5500"
- />
- </Shape>
In versions above android3.0, google solves this problem for us, so we only need to write according to the normal situation, and put the file under res/drawable-v12 on the line, as shown below:
[Html]View plaincopy
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Shape xmlns: android = "http://schemas.android.com/apk/res/android">
- <Corners
- Android: topLeftRadius = "5dp"
- Android: bottomLeftRadius = "5dp"/>
- <Stroke
- Android: width = "1dp"
- Android: color = "# FF5500"
- />
- </Shape>