在Android的圆形按钮 [英] Rounded Button in Android

查看:114
本文介绍了在Android的圆形按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android程序中创建的圆形按钮。我已经看过<一href="http://stackoverflow.com/questions/3646415/how-to-create-edittext-with-rounded-corners/3646629#3646629">How与圆角创建的EditText?

我想实现的是:

  1. 在圆边按钮
  2. 更改按钮背景/在不同状态的外观(像的onclick,聚焦)
  3. 使用我自己的PNG的背景,而不是创建一个形状。
解决方案

您可以做一个圆角按钮,无需求助于ImageView的。

一个背景选择资源, button_background.xml

 &LT; XML版本=1.0编码=UTF-8&GT?;
     &LT;选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;
     &LT;! - 非重点国家
       - &GT;
      &LT;项目安卓state_focused =假的Andr​​oid版本:state_selected =假的Andr​​oid版本:STATE_ pressed =假机器人:可绘制=@可绘制/ button_unfocused/&GT;
      &LT;项目安卓state_focused =假的Andr​​oid版本:state_selected =真正的机器人:STATE_ pressed =假机器人:可绘制=@可绘制/ button_unfocused/&GT;
     &LT;! - 聚焦状态
       - &GT;
      &LT;项目安卓state_focused =真正的机器人:state_selected =假的Andr​​oid版本:STATE_ pressed =假机器人:可绘制=@可绘制/ button_focus/&GT;
      &LT;项目安卓state_focused =真正的机器人:state_selected =真正的机器人:STATE_ pressed =假机器人:可绘制=@可绘制/ button_focus/&GT;
     &LT;  - !pressed
       - &GT;
      &LT;项目的android:STATE_ pressed =真正的机器人:可绘制=@可绘制/ button_ preSS/&GT;
    &LT; /选择器&GT;
 

对于每一个国家,一个绘制资源,如button_ press.xml:

 &LT;形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android机器人:形状=矩形&GT;
  &LT;行程机器人:宽=1DP机器人:颜色=#FF404040/&GT;
  &LT;边角机器人:半径=6DP/&GT;
  &LT;梯度安卓startColor =#FF6800机器人:centerColor =#FF8000机器人:endColor =#FF9700机器人:角=90/&GT;
&LT; /形状&GT;
 

注意角属性,这可以让你圆润的边角

 安卓背景=@可绘制/ button_background
 

I want to create rounded buttons in an Android program. I have looked at How to create EditText with rounded corners?

What I want to achieve is:

  1. Rounded Edge Buttons
  2. Change Button background/appearance on different states (Like Onclick, Focus)
  3. Use my own PNG for the background and not create a shape.

解决方案

You can do a rounded corner button without resorting to an ImageView.

A background selector resource, 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>

For each state, a drawable resource, e.g. 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>

Note the corners attribute, this gets you rounded corners

android:background="@drawable/button_background"

这篇关于在Android的圆形按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆