0
点赞
收藏
分享

微信扫一扫

Android中利用LinearLayout继承实现ImageButton


原理:通过继承Linearlayout,摆放自己所需的imageview和textview,形成ImageButton

直接上源码:

  

​​view plain​​​ ​​​copy to clipboard​​​ ​​​print​​​ ​​​?​​

1. import android.widget.TextView;
2.
3. public class
4. {
5. private
6. private
7.
8. public
9. {
10. super(context,attrs);
11.
12. new
13. mImage.setPadding(0,0,0,0);
14. new
15. //mText.setGravity(android.view.Gravity.CENTER_HORIZONTAL);
16. // mText.setGravity(android.view.Gravity.CENTER_VERTICAL);
17. mText.setPadding(0,0,0,0);
18.
19.
20. true);
21. true);
22. setBackgroundResource(android.R.drawable.btn_default);
23. setOrientation(LinearLayout.VERTICAL);
24. addView(mImage);
25. addView(mText);
26. }
27. }

 

调用自己编写的ImageButton1

 

​​view plain​​​ ​​​copy to clipboard​​​ ​​​print​​​ ​​​?​​

1. <com.test.b.ImageButton1
2. android:id="@+id/imbtn01"
3. android:layout_width="wrap_content"
4. android:layout_height="wrap_content"
5. android:src="@drawable/icon"
6. android:text="MOAR"
7. android:textColor="#ff000000"
8. />
<com.test.b.ImageButton1 android:id="@+id/imbtn01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" android:text="MOAR" android:textColor="#ff000000" />

注意调用ImageButton1时,要用全名:com.test.b.ImageButton1 

 

 

效果:button中上图下文字

 

 

Android中利用LinearLayout继承实现ImageButton_android

 

举报

相关推荐

0 条评论