Android中的圆形button

我想在Android程序中创build四舍五入的button。 我看过如何创build圆angular的EditText?

我想要达到的是:

  1. 圆形的边缘button
  2. 更改不同状态下的button背景/外观(如Onclick,Focus)
  3. 使用我自己的PNG作为背景,而不是创build一个形状。

你可以做一个圆angularbutton,而不诉诸ImageView。

后台select器资源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> 

注意angular落的属性,这让你圆angular

 android:background="@drawable/button_background" 

如果在Android中需要一个四舍五入的button,那么创build一个XML文件“RoundShapeBtn.xml”作为drawable。

  <?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> 

添加到您的button代码:

 android:background="@drawable/RoundShapeBtn" 

Googlebuild议您不要模仿其他平台的UI元素。 我不会在Android应用程序中放置四舍五入的iOS样式button。

在android中的可绘制文件夹中创buildxml文件,如:

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背景。

  <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"/> 

像这样扩展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); } } 

并应用您的正常的背景资源,它应该被削减圆angular。

这可以使用corner属性来完成。 看看下面的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> 

将button状态和形状放在1个xmlselect器文件中好多了。 这应该会让你的应用运行得更快更好。 试试这个(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> 

PS-devise提示:渐变和圆angular矩形最好使用,当你几乎不知道他们在那里 – 明智地使用。