zoukankan      html  css  js  c++  java
  • 自定义带图片和文字的ImageTextButton

    今天我们来讲一下有关自定义控件的问题,今天讲的这篇是从布局自定义开始的,难度不大,一看就明白,估计有的同学或者开发者看了说,这种方式多此一举,但是小编我不这么认为,多一种解决方式,就多一种举一反三的学习。下一次或者过几天我会从自定义属性,在布局文件中使用属性的方式再讲一篇关于自定义控件的文章,希望对大家能够有所帮助。


    现在开始讲自定义带图片和文字的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;
    
    	@Override
    	protected 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 ,公众号[非著名程序员]可能是东半球最好的技术分享公众号。每天,每周定时推送一些有关移动开发的原创文章和教程。

  • 相关阅读:
    Firefly是什么?有什么特点?
    windows7下启动mysql服务出现服务名无效
    win7系统64位eclipse环境超详细暗黑1.4服务器搭建
    Python安装模块出错(ImportError: No module named setuptools)解决方法
    Error format not a string literal and no format arguments解决方案
    DropFileName = "svchost.exe" 问题解决方案
    javascript
    Javascript
    PHP 命名空间namespace 和 use
    css
  • 原文地址:https://www.cnblogs.com/loonggg/p/4981780.html
Copyright © 2011-2022 走看看