实现ImageButton的两种状态的切换功能(按下和弹起):
1.在res-->drawable-hdpi中新建add_button_changed.xml布局文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/add" />
<item android:state_pressed="true" android:drawable="@drawable/btn_follow_press" />
<item android:drawable="@drawable/add" />
</selector>
2.在activity_main.xml文件中添加该布局文件(包含按键的两种形式)
<include
android:id="@+id/header_layout"
layout="@layout/head"/>
<RelativeLayout
android:id="@+id/add_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<ImageView
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/add_button_changed"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:shadowDx="0"
android:shadowDy="-1"
android:shadowRadius="0.2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/add"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:text="新建便签"
android:textSize="13dp"
android:textColor="#80808080"/>
</RelativeLayout>
3. 在MainActivity.java文件中将其实例化
add = (ImageView) findViewById(R.id.add);
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, AddNew.class);
startActivity(intent);
}
});
1.在res-->drawable-hdpi中新建add_button_changed.xml布局文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/add" />
<item android:state_pressed="true" android:drawable="@drawable/btn_follow_press" />
<item android:drawable="@drawable/add" />
</selector>
2.在activity_main.xml文件中添加该布局文件(包含按键的两种形式)
<include
android:id="@+id/header_layout"
layout="@layout/head"/>
<RelativeLayout
android:id="@+id/add_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<ImageView
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/add_button_changed"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:shadowDx="0"
android:shadowDy="-1"
android:shadowRadius="0.2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/add"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:text="新建便签"
android:textSize="13dp"
android:textColor="#80808080"/>
</RelativeLayout>
3. 在MainActivity.java文件中将其实例化
add = (ImageView) findViewById(R.id.add);
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, AddNew.class);
startActivity(intent);
}
});