Android中利用LinearLayout继承实现ImageButton 转
2012-06-29 22:21
483 查看
原理:通过继承Linearlayout,摆放自己所需的imageview和textview,形成ImageButton
直接上源码:
[c-sharp] view plaincopy
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
[xhtml] view plaincopy
<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中上图下文字
直接上源码:
[c-sharp] view plaincopy
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
[xhtml] view plaincopy
<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 利用TwinklingRefreshLayout 实现下拉效果
- [转]用android LinearLayout和RelativeLayout实现精确布局
- [Android]继承上下文类和利用meta-data实现默认对象
- android LinearLayout和RelativeLayout实现精确布局
- 用android LinearLayout和RelativeLayout实现精确布局
- android动态布局:用java代码实现LinearLayout以及TextView
- android LinearLayout和RelativeLayout实现精确布局
- android用Adapter实现ListView的初始化报错android.widget.LinearLayout cannot be cast to android.widget.TextView
- android LinearLayout和RelativeLayout实现精确布局
- android LinearLayout和RelativeLayout实现精确布局
- Android中利用DrawerLayout+NavigationView实现滑动菜单效果
- android开发利用tablelayout布局实现登录界面
- 利用LinearLayout实现屏幕分割
- android LinearLayout和RelativeLayout实现精确布局
- (转)用android LinearLayout和RelativeLayout实现listView的listItem布局
- Android利用DrawerLayout实现侧滑菜单
- Android中利用LinearLayout动态添加控件