android 按钮变化颜色
res/layout/activity_main.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn1"
android:text="123456"
android:layout_width="180dp"
android:layout_height="45dp"
android:layout_marginLeft="70dp"
android:background="@drawable/login_button_selector"
android:layout_marginTop="40dp"
android:textColor="@android:color/holo_red_light" />
</LinearLayout>
res/drawable/login_button_selector.xml
<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>
res/values/strings.xml
<resources>
<string name="app_name">testapp</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<drawable name="clr_normal">#D200D2</drawable>
<drawable name="clr_pressed">#FFFF37</drawable>
</resources>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
res/layout/activity_main.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn1"
android:text="123456"
android:layout_width="180dp"
android:layout_height="45dp"
android:layout_marginLeft="70dp"
android:background="@drawable/login_button_selector"
android:layout_marginTop="40dp"
android:textColor="@android:color/holo_red_light" />
</LinearLayout>
res/drawable/login_button_selector.xml
<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>
res/values/strings.xml
<resources>
<string name="app_name">testapp</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<drawable name="clr_normal">#D200D2</drawable>
<drawable name="clr_pressed">#FFFF37</drawable>
</resources>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}