1.自定义一个类 继承LinearLayout
4.效果
public ImageButton_define(Context context,AttributeSet attrs) {
super(context, attrs);
imageViewbutton = new ImageView(context, attrs);
imageViewbutton.setPadding(0, 0, 0, 0);
textView = new TextView(context, attrs);
textView.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
textView.setPadding(0, 0, 0, 0);
setClickable(true);
setFocusable(true);
setBackgroundResource(android.R.drawable.btn_default);
setOrientation(LinearLayout.VERTICAL);
addView(textView);
addView(imageViewbutton);
}
2.建一个自定义defined.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.example.imagebutton.ImageButton_define
android:id="@+id/define_iamgebutton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000"
android:src="@null"
android:text="图片文本"
android:textColor="#cc0000" >
</com.example.imagebutton.ImageButton_define>
</LinearLayout>
3.在主类加载
setContentView(R.layout.difined);
4.效果