Android中利用LinearLayout继承实现ImageButton
2016-12-02 14:54
441 查看
原理:通过继承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);
}
}
[c-sharp]
view plain
copy
print?
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" /> [xhtml] view plain copy print? <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和RelativeLayout实现精确布局
- android LinearLayout和RelativeLayout实现精确布局
- android LinearLayout和RelativeLayout实现精确布局
- 用android LinearLayout和RelativeLayout实现精确布局
- android LinearLayout和RelativeLayout实现精确布局
- Android中利用LinearLayout动态添加控件
- 用android LinearLayout和RelativeLayout实现精确布局
- android LinearLayout和RelativeLayout实现精确布局
- (转)用android LinearLayout和RelativeLayout实现listView的listItem布局
- android LinearLayout和RelativeLayout实现精确布局
- android LinearLayout和RelativeLayout实现精确布局
- 利用LinearLayout实现屏幕分割
- Android开发布局系列: LinearLayout布局实现垂直水平居中
- android LinearLayout和RelativeLayout实现精确布局
- Android中利用LinearLayout动态添加控件
- android LinearLayout和RelativeLayout实现精确布局
- android动态布局:用java代码实现LinearLayout以及TextView