android Button ImageButton 区别
BUTTON类结构图:

由类结构图,可以看出Button是继承于TextView的,所以TextView的一些属性也适用于Button控件。
ImageButton类结构图:
ImageButton就是用一个图标代表了一些文字,它没Android:text属性。它由Android:src指定图标的位置
android:src="@drawable/back_48"
其他属性都和Button差不多
1、 如何设置按钮的样式?
通过Android:background设置
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#fff000" />
2、如何设置背景图标:
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textStyle="bold" android:background="@drawable/back_48"
/>
3、 如何设置按钮的文字颜色
通过Android:textColor
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textColor="#ff0000" />
nbsp;
4、 如何设置按钮的文字样式
通过android:textStyle
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textColor="#ff0000" android:textStyle="bold" />
5、 如何为按钮添加监听器注册事件
方式1:通过setOnClickListener方式
myBtn4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myBtn4.setText("setOnclickListener事件监听注册成功");
}
});
实践演练:
1、 怎么样设置ImageButton的图标位置
android:layout_width="wrap_content" android:layout_height="wrap_content" />
2、 怎么样为ImageButton添加监听器注册事件
与Button一样它照样有两种方式设置
方式1:通过onClickListener
imgBtn01.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myTextView.setText("ImageButton的监听事件");
}
});
方式2:通过XML文件设置
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:onClick="ImageButtonXml" />
1、 设置透明度
imgBtn01.setAlpha(50);//设置透明度