【发布时间】:2012-03-09 05:29:20
【问题描述】:
我想在 Android 程序中创建圆形按钮。我看过How to create EditText with rounded corners?
我想要实现的是:
- 圆边按钮
- 更改按钮在不同状态下的背景/外观(如 Onclick、Focus)
- 使用我自己的 PNG 作为背景,而不是创建形状。
【问题讨论】:
标签: android android-button rounded-corners
我想在 Android 程序中创建圆形按钮。我看过How to create EditText with rounded corners?
我想要实现的是:
【问题讨论】:
标签: android android-button rounded-corners
您可以在不借助 ImageView 的情况下制作圆角按钮。
一个背景选择器资源,button_background.xml:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states
-->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/button_unfocused" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/button_unfocused" />
<!-- Focused states
-->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/button_focus" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/button_focus" />
<!-- Pressed
-->
<item android:state_pressed="true" android:drawable="@drawable/button_press" />
</selector>
对于每个状态,一个可绘制资源,例如button_press.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="#FF404040" />
<corners android:radius="6dp" />
<gradient android:startColor="#FF6800" android:centerColor="#FF8000" android:endColor="#FF9700" android:angle="90" />
</shape>
注意corners 元素,这会让你得到圆角!
然后在按钮上设置背景drawable:
android:background="@drawable/button_background"
编辑 (9/2018):同样的技术可用于创建圆形按钮。圆形实际上只是一个方形按钮,其半径大小设置为方形边的 1/2
此外,在上面的示例中,stroke 和 gradient 不是必需元素,它们只是示例和您可以看到圆角形状的方式
【讨论】:
drawable 文件夹?我有四个不同的drawable 文件夹:drawable-hdpi; drawable-mdpi; drawable-xhdpi; drawable-xxhdpi.
如果您需要 Android 中的圆形按钮,则创建一个 XML 文件“RoundShapeBtn.xml”作为可绘制对象。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp">
<solid android:color="#6E6E6E"/> <!-- this one is ths color of the Rounded Button -->
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
将此添加到您的按钮代码中:
android:background="@drawable/RoundShapeBtn"
【讨论】:
在 android 的 drawable 文件夹中创建 xml 文件:
rounded_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp"/> // if you want clear round shape then make radius size is half of your button`s height.
<solid android:color="#EEFFFFFF"/> // Button Colour
<padding
android:bottom="5dp"
android:left="10dp"
android:right="10dp"
android:top="5dp"/>
</shape>
现在这个 xml 文件作为你的按钮背景。
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/rounded_button"
android:text="@string/button_text"
android:textColor="@color/black"/>
【讨论】:
solid android:color,而是使用 android:backgroundTint 为每个 Button 指定颜色
像这样扩展ImageView:
public class RoundedImageView extends ImageView {
private static final String TAG = "RoundedImageView";
private float mRadius = 0f;
public RoundedImageView(Context context) {
super(context);
}
public RoundedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
// retrieve styles attributes
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedView);
mRadius = a.getDimension(R.styleable.RoundedView_radius, 0f);
a.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
// only do this if we actually have a radius
if(mRadius > 0) {
RectF rect = new RectF(0, 0, getWidth(), getHeight());
Path clipPath = new Path();
clipPath.addRoundRect(rect, mRadius, mRadius, Path.Direction.CW);
canvas.clipPath(clipPath);
}
super.onDraw(canvas);
}
}
然后将你的普通背景资源应用到它上面,它应该用圆角剪裁。
【讨论】:
最好将按钮状态和形状放在 1 个 xml 选择器文件中。这应该使您的应用程序运行得更快/更好。试试这个(由 Android 应用程序开发简介提供)。在这里不发送垃圾邮件只是显示这不是我的代码。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="12dip" />
<stroke android:width="1dip" android:color="#333333" />
<gradient android:angle="-90" android:startColor="#333333" android:endColor="#555555" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="12dip" />
<stroke android:width="1dip" android:color="#333333" />
<solid android:color="#58857e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="12dip" />
<stroke android:width="1dip" android:color="#333333" />
<gradient android:angle="-90" android:startColor="#333333" android:endColor="#555555" />
</shape>
</item>
</selector>
P.S. 设计提示:渐变和圆角矩形最好在您无法判断它们的存在时使用 - 明智地使用。
【讨论】:
It is recommended by Google 你不会模仿其他平台的 UI 元素。我不会在 Android 应用程序中放置圆形 iOS 样式按钮。
【讨论】:
这可以使用角属性来完成。看下面的xml。
<item>
<shape android:shape="rectangle" >
<stroke
android:height="1.0dip"
android:width="1.0dip"
android:color="#ffee82ee" />
<solid android:color="#ffee82ee" />
<corners
android:bottomLeftRadius="102.0dip"
android:bottomRightRadius="102.0dip"
android:radius="102.0dip"
android:topLeftRadius="102.0dip"
android:topRightRadius="102.0dip" />
</shape>
</item>
【讨论】:
圆形按钮也可以使用环形drawable创建,参见http://www.zoftino.com/android-shape-drawable-examples
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thickness="40dp"
android:useLevel="false">
<solid android:color="@color/colorPrimary" />
<padding android:bottom="50dp"
android:left="16dp"
android:right="16dp"
android:top="50dp"/>
</shape>
【讨论】:
创建名为 btnOval 的 Drawable 资源-->然后是过去的代码;
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:padding="10dp">
<solid android:color="#6E6E6E"/>
</shape>
和按钮标签内的用户,
<Button
andorid:width=""
android:hieght=""
android:background="@Drawable/btnOval"
/>
【讨论】:
试试下面的代码 创建一个名为 circular_button.xml 的可绘制文件并插入下面的
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#008577" />
<corners android:bottomRightRadius="100dp"
android:bottomLeftRadius="100dp"
android:topRightRadius="100dp"
android:topLeftRadius="100dp"/>
</shape>
然后把按钮的背景改成这个drawable文件
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/circle_button"
android:text="Button"/>
如果你想要一个完整的圆形按钮,你可以使用下面的 drawable
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#008577"/>
<size
android:width="120dp"
android:height="120dp"/>
</shape>
【讨论】: