您的位置:首页 > 移动开发 > Android开发

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

2015-05-16 11:06 323 查看

顶栏封装

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

懒,所以随便贴个代码~

链接

↑原文

布局文件中的:

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


结束~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: