Android中利用LinearLayout继承实现ImageButton
2011-07-22 18:17
483 查看
原理:通过继承Linearlayout,摆放自己所需的imageview和textview,形成ImageButton
直接上源码:
view plaincopy to clipboardprint?import android.widget.TextView;
public class ImageButton1 extends LinearLayout
{
private ImageView mImage;
private TextView mText;
public ImageButton1(Context context, AttributeSet attrs)
{
super(context,attrs);
mImage = new ImageView(context,attrs);
mImage.setPadding(0,0,0,0);
mText = new TextView(context,attrs);
//mText.setGravity(android.view.Gravity.CENTER_HORIZONTAL);
// mText.setGravity(android.view.Gravity.CENTER_VERTICAL);
mText.setPadding(0,0,0,0);
setClickable(true);
setFocusable(true);
setBackgroundResource(android.R.drawable.btn_default);
setOrientation(LinearLayout.VERTICAL);
addView(mImage);
addView(mText);
}
}
注意调用ImageButton1时,要用全名:com.test.b.ImageButton1
效果:button中上图下文字
直接上源码:
view plaincopy to clipboardprint?import android.widget.TextView;
public class ImageButton1 extends LinearLayout
{
private ImageView mImage;
private TextView mText;
public ImageButton1(Context context, AttributeSet attrs)
{
super(context,attrs);
mImage = new ImageView(context,attrs);
mImage.setPadding(0,0,0,0);
mText = new TextView(context,attrs);
//mText.setGravity(android.view.Gravity.CENTER_HORIZONTAL);
// mText.setGravity(android.view.Gravity.CENTER_VERTICAL);
mText.setPadding(0,0,0,0);
setClickable(true);
setFocusable(true);
setBackgroundResource(android.R.drawable.btn_default);
setOrientation(LinearLayout.VERTICAL);
addView(mImage);
addView(mText);
}
}
view plaincopy to clipboardprint?<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" /> <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中利用LinearLayout继承实现ImageButton 转
- Android中利用LinearLayout继承实现ImageButton
- Android学习——LinearLayout布局实现居中、左对齐、右对齐
- android LinearLayout和RelativeLayout实现精确布局
- Android进阶之利用Tablayout+ViewPager+Fragment实现神奇的滑动效果
- 用android LinearLayout和RelativeLayout实现精确布局
- android中利用tablelayout实现表格效果
- Android LinearLayout实现下拉刷新
- Android-利用LinearGradient实现文字一闪一闪
- android自定义view继承linearLayout,view大小大于屏幕
- android 较高版本,页面分割线实现LinearLayoutCompat布局简单事例
- Android利用FlexboxLayout轻松实现流动布局
- android LinearLayout和RelativeLayout实现精确布局
- android利用draglayout实现菜单栏顶部悬浮效果
- android LinearLayout和RelativeLayout实现精确布局
- Android 使用线性布局LinearLayout和Button实现一个点红块游戏
- android动态布局:用java代码实现LinearLayout以及TextView
- Android使用共享元素实现转场动画 错误:Unable to create layer for LinearLayout
- Android LinearLayout实现多行多列