您的位置:首页 > 移动开发 > Android开发

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);
}
}
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中上图下文字



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