您的位置:首页 > 其它

实现ImageButton里放置文字

2015-09-07 10:24 337 查看
1.自定义一个类 继承LinearLayout
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.效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: