在res/drawable创建两个样式 点击前/点击后
round:
1 <?xml version="1.0" encoding="utf-8"?>
2
3
4
6 android:endColor="#E6E6FA"
7 android:angle="0" />
8
10 android:color="#D2691E"
11 />
12
13
15 android:top="10dp"
16 android:right="10dp"
17 android:bottom="10dp"
18 />
19
newround:
1 <?xml version="1.0" encoding="utf-8"?>
2
3
4
6 android:endColor="#f5deb3"
7 android:angle="0" />
8
10 android:color="#FFB6C1"
11 />
12
13
15 android:top="10dp"
16 android:right="10dp"
17 android:bottom="10dp"
18 />
19
接下来j
Activity:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/asdf"
tools:context="com.example.top5_4.MainActivity" >
android:id="@+id/but"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="点击按钮改变按钮样式"
android:background="@drawable/round"
/>
JAVA:
1 public classMainActivity extends Activity {2 privateButton but;3 @Override4 protected voidonCreate(Bundle savedInstanceState) {5 super.onCreate(savedInstanceState);6 setContentView(R.layout.activity_main);7 but=(Button) findViewById(R.id.but);8 but.setOnClickListener(newOnClickListener() {9
10 @Override11 public voidonClick(View v) {12 //TODO Auto-generated method stub13 //点击后更改按钮样式
14 but.setBackgroundResource(R.drawable.newround);15 //另外设置按钮的透明度setAlpha()里面的值大小为0~255
16 but.getBackground().setAlpha(99);17 }18 });19 }20 }
运行效果:
点击之前
点击后: