android提高——自定义控件(button)

博主最近研究了下自定义控件的button相关的内容,觉得不做下笔记对不起自己,就怕过些天就淡忘了,就和大家分享下:

一、自定义出来的效果如下:


效果:三个按键,正常状态是粉红色的背景,黑色字体;当button被按下时,背景变为蓝色,字体变为红色。


二、实现的步骤如下:

1、字体颜色改变的实现:

1)创建文件:res/color/text_clr.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffff0000"/> <!-- pressed red-->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused bule-->
    <item android:color="#ff000000"/> <!-- default black-->
</selector>

2)在所定义的button按键添加如下属性:

android:textColor="@color/text_clr"


完成以上两步即可实现按键在不同状态字体的颜色变化。

具体文档请参考:http://developer.android.com/guide/topics/resources/color-list-resource.html

下面再实现背景的切换。


2、button背景改变的实现(与1略有不同):

1)、先在res/value目录下定义专门存放一个颜色值的文件:colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <drawable name="bk_press">#0ff</drawable>	<!-- press pick-->
    <drawable name="bk_foucus">#f00</drawable>	<!-- foucus bule-->
    <drawable name="bk_normal">#ECA8B3</drawable>	<!-- default pick-->
</resources>

2)、创建文件 res/drawable/button_bg.xml :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/bk_press" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/bk_foucus" /> <!-- focused -->
    <item android:drawable="@drawable/bk_normal" /> <!-- default -->
</selector>


3) 在所定义的button按键添加如下背景属性:

android:background="@drawable/button_bg"


3、提供下效果图的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:gravity="center_vertical">

    <Button
        android:id="@+id/bt1"
        android:layout_marginTop="5dip"
        android:layout_centerHorizontal="true"
        android:text="@string/but1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/text_clr"
        android:background="@drawable/button_bg"
        />
    <Button
        android:id="@+id/bt2"
        android:layout_marginTop="5dip"
        android:layout_centerHorizontal="true"
        android:text="@string/but2"
        android:layout_below="@id/bt1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/text_clr"
        android:background="@drawable/button_bg"
        />
    <Button
        android:id="@+id/bt3"
        android:layout_marginTop="5dip"
        android:layout_centerHorizontal="true"
        android:text="@string/but3"
        android:layout_below="@id/bt2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/text_clr"
        android:background="@drawable/button_bg"
        />

</RelativeLayout>


参考文档:http://developer.android.com/guide/topics/resources/drawable-resource.html

完成以上步骤,大功告成~


备注:在color和drawable文件夹定义的只有文件名称会生成在R文件中生成相应的id;而在value文件夹定义的,每个节点都会在R文件生成相应的id。








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
设计自定义件需要以下步骤: 1.创建一个新的 Android Studio 项目。 2.在项目中创建自定义件的类。该类应继承自 View 或其子类(例如 Button、EditText 等)。 3.在自定义件类中实现构造函数和必要的方法,例如 onMeasure()、onLayout() 和 onDraw() 等。 4.在布局文件中使用自定义件。 下面是一个示例代码: ``` public class MyCustomView extends View { private Paint paint; private Rect rect; public MyCustomView(Context context) { super(context); init(); } public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { paint = new Paint(); paint.setColor(Color.BLUE); rect = new Rect(0, 0, 100, 100); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int desiredWidth = 200; int desiredHeight = 200; int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int width; int height; if (widthMode == MeasureSpec.EXACTLY) { width = widthSize; } else if (widthMode == MeasureSpec.AT_MOST) { width = Math.min(desiredWidth, widthSize); } else { width = desiredWidth; } if (heightMode == MeasureSpec.EXACTLY) { height = heightSize; } else if (heightMode == MeasureSpec.AT_MOST) { height = Math.min(desiredHeight, heightSize); } else { height = desiredHeight; } setMeasuredDimension(width, height); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawRect(rect, paint); } } ``` 在布局文件中使用自定义件: ``` <com.example.myapplication.MyCustomView android:layout_width="wrap_content" android:layout_height="wrap_content"/> ``` 以上代码是一个简单的自定义 View,它绘制一个蓝色的矩形。当然,你可以根据需要修改自定义件的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值