自定义带图片和文字的ImageTextButton
来源:互联网 发布:cocos2dx for mac 编辑:程序博客网 时间:2024/05/17 04:05
今天我们来讲一下有关自定义控件的问题,今天讲的这篇是从布局自定义开始的,难度不大,一看就明白,估计有的同学或者开发者看了说,这种方式多此一举,但是小编我不这么认为,多一种解决方式,就多一种举一反三的学习。下一次或者过几天我会从自定义属性,在布局文件中使用属性的方式再讲一篇关于自定义控件的文章,希望对大家能够有所帮助。
现在开始讲自定义带图片和文字的ImageTextButton的实现方法。
第二步:自定义一个类ImageTextButton继承LinearLayout
第三步:自定义控件的使用,在布局文件activity_main.xml中的使用
第四步:在Activiy中的使用
到这里就讲完了,其实看完代码就知道了,非常简单。大家可以试一试哦!获取demo的方法跟以前一样,只需在公众号里回复关键字“2”即可获得。
现在开始讲自定义带图片和文字的ImageTextButton的实现方法。
效果图如下:
第一步:新建一个image_text_buttton.xml的布局文件,供自定义的控件使用
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="12dp" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="8dp" android:textColor="#000000" /></LinearLayout>
第二步:自定义一个类ImageTextButton继承LinearLayout
package net.loonggg.itbd.view;import net.loonggg.itbd.R;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class ImageTextButton extends LinearLayout {private ImageView iv;private TextView tv;public ImageTextButton(Context context) {super(context);}public ImageTextButton(Context context, AttributeSet attrs) {super(context, attrs);LayoutInflater.from(context).inflate(R.layout.image_text_buttton, this,true);iv = (ImageView) findViewById(R.id.iv);tv = (TextView) findViewById(R.id.tv);}public void setDefaultImageResource(int resId) {iv.setImageResource(resId);}public void setDefaultTextViewText(String text) {tv.setText(text);}/** * @param resId */public void setImageResource(int resId) {iv.setImageResource(resId);}/** * @param text */public void setTextViewText(String text) {tv.setText(text);}/** * @param color */public void setTextColor(int color) {tv.setTextColor(color);}}
第三步:自定义控件的使用,在布局文件activity_main.xml中的使用
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <net.loonggg.itbd.view.ImageTextButton android:id="@+id/itb" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
第四步:在Activiy中的使用
public class MainActivity extends Activity {private ImageTextButton itb;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);itb = (ImageTextButton) findViewById(R.id.itb);itb.setImageResource(R.drawable.sure);itb.setTextViewText("确定");}}
到这里就讲完了,其实看完代码就知道了,非常简单。大家可以试一试哦!获取demo的方法跟以前一样,只需在公众号里回复关键字“2”即可获得。
微信公众号:smart_android ,公众号[非著名程序员]可能是东半球最好的技术分享公众号。每天,每周定时推送一些有关移动开发的原创文章和教程。
0 0
- 自定义带图片和文字的ImageTextButton
- 自定义Android带图片和文字的ImageButton
- 【自定义Android带图片和文字的ImageButton】
- 自定义Android带图片和文字的ImageButton
- 自定义Android带图片和文字的ImageButton
- 【自定义Android带图片和文字的ImageButton】
- Android实现自定义带文字和图片的Button
- Android实现自定义带文字和图片的Button
- Android实现自定义带文字和图片的Button
- Android实现自定义带文字和图片的Button
- Androidの实现自定义带文字和图片的Button
- Android实现自定义带文字和图片的Button
- 【自定义Android带图片和文字的ImageButton】
- Android实现自定义带文字和图片的Button
- Android实现自定义带文字和图片的Button
- Android实现自定义带文字和图片的Button
- Android实现自定义带文字和图片的Button
- Android实现自定义带文字和图片的Button
- zzuli OJ 1113: 递归调用的次数统计(函数专题)
- Android高效加载图片,有效避免程序OOM
- windows2008r2系统破解登录密码方法
- 解决easyui 1.2.4+jQuery1.6的dialog无法刷新的问题
- ExtJs通过AJAX发送Post给django后台报错403!
- 自定义带图片和文字的ImageTextButton
- iOS 多线程开发
- 利用zxing和zbar 实现扫一扫功能
- jquery插件开发笔记
- 使用myeclipse登录code csdn
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest(G - The Problem to Slow Down You-回文树)
- Linux - vncaddrbook错误排查(Permission denied )
- 挖掘机(DMS)(服务器与客户端读写文件,收发数据)
- LeetCode---Lowest Common Ancestor of a Binary Search Tree