Button点击效果及自定义控件

技术点:

1.实现Button的3种状态,无点击,按住没松开,点击过后(点击前后一般都应该是同一种状态)

2.做成自定义控件,一次制造,到处使用

步骤:

1.在layout文件夹创建一个mybutton.xml,作为自定义button的布局,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
<Button
    android:background="@drawable/button_file"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

2.在drawable文件夹创建button_file.xml文件,作为自定义button点击效果切换图

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/abit" android:state_pressed="true" />
    <item android:drawable="@drawable/abit" android:state_focused="true" />
    <item android:drawable="@drawable/bbit" android:state_pressed="false" />
    <item android:drawable="@color/colorPrimary" />

</selector>

3.创建一个MyButton.java继承 LinearLayout

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
public class MyButton extends LinearLayout {

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.button_layout,this);
    }
}

4.准备工作已经做完,下面就可以在活动中直接使用这个MyButton了,先在xml中布置MyButton,然后就可以在活动中直接使用了

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.moumou.myapplication.MainActivity">

    <com.moumou.myapplication.MyButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></com.moumou.myapplication.MyButton>

</LinearLayout>

 

 
 

 

转载于:https://www.cnblogs.com/MoneyBender/p/6608011.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值