[2015/05/16] 顶栏封装 & Android按钮按下改变颜色

顶栏封装

其实我觉得我应该。恩。顶栏。。。

懒,所以随便贴个代码~

链接

↑原文


布局文件中的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.moplast.app"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- 顶栏 -->

    <com.moplast.custom.TopBar
        android:id="@+id/topBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#000000"
        custom:leftText="返回"
        custom:leftTextColor="#FFFFFF"
        custom:leftTextSize="12sp"
        custom:rightText=""
        custom:rightTextColor="#FFFFFF"
        custom:rightTextSize="20sp"
        custom:title="我的当前位置"
        custom:titleTextColor="#FFFFFF"
        custom:titleTextSize="15sp" >
    </com.moplast.custom.TopBar>


Android按钮按下改变颜色

提供两种方法:


第一种:手动设置


backButton.setOnTouchListener(new OnTouchListener() {
				public boolean onTouch(View v, MotionEvent event) {
					//按钮按下时更改视图
					if(event.getAction() == MotionEvent.ACTION_DOWN){
						backButton.setBackgroundResource(R.drawable.green_click);
					}//按钮松开时恢复默认视图
					else if(event.getAction()==MotionEvent.ACTION_UP){
						backButton.setBackgroundResource(R.drawable.green);
					}
					return false;
				}
			});

这样每个按钮都需要设置监听器,非常麻烦= =。。所以我又找了个新办法,配置文件是老大!


第二种:配置文件

按下改变颜色

↑原文来自以上。

首先,在res文件夹下新建一个文件夹drawable,这是无关分辨率的:

在下面建立一个xml文件:login_button_selector.xml(我的按钮是分颜色的。。所以我的名字是button_selector_green)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

        <item 
            android:drawable="@drawable/clr_normal" 
            android:state_pressed="false"/>
        <item 
            android:drawable="@drawable/clr_pressed" 
            android:state_pressed="true"/>

    </selector>
然后在value文件夹下的string.xml文件里添加:

    <drawable name="clr_normal">#1BE475</drawable>
    <drawable name="clr_pressed">#33CC66</drawable>

最后在布局文件中为button添加:

android:background="@drawable/button_selector_green"

结束~





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值