Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

Is it possible to specify corner radius as a percentage in an Android drawable?

As an Android drawable, this represents a black rectangle with rounded corners.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20dp" />
    <solid android:color="#000000" />
</shape>

However, the corner radius is specified as an absolute 20dp, so if this same drawable is shown at different sizes, it appears differently. The smaller shape is not just a "scaled down" version of the larger one. Instead, it's "rounder" and the larger one is "more square" because the border radius is a static 20dp regardless of the size of the drawable.

absolute border radius

I want to specify the radius relative to the size of the full drawable, so when it is drawn at different sizes each one appears as a scaled up/down version of the others.

relative border radius

I'm more familiar with CSS, where this can be done in one line:

border-radius: 20%;

I'm surprised to find Android lacking this CSS simplicity. Android does not recognize % as a unit.

<corners android:radius="20%" />

Is there some simple way to achieve my desired result in Android?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...