搜索
您的当前位置:首页正文

Android自定义组合控件(一)

来源:独旅网

最近参加济南一分享会,感受颇深,也很欣赏大神们的分享精神,好东西大家一起分享

不能做只会搬砖的码农,要成为一个真正的程序员,当然我是媛,程序员分为三类,初级程序员,中级程序员,高级程序员,也不能总是做个小菜鸟,那也太没有追求了,为了我的大神梦,开始学习自定义控件

本博客从最简单的开始,先来介绍自定义组合控件,在我看来自定义空间中组合控件是最简单的,这里拿最常见的图片和文字组合来说明

先上界面:

这个界面还是很常见的,很多app主页都是这么界面,当然下面的图片文字等是可以自己设置的

下面是自定义组合控件的主要代码:

首先是继承自LinearLayout自定义控件

package com.sdufe.thea.guo.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sdufe.thea.guo.R;

public class CustomView extends LinearLayout {
	
	private ImageView mImageView;
	private TextView mTextView;
	Context context;


	public CustomView(Context context) {
		super(context,null);
	}

	public CustomView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context=context;
		View view=LayoutInflater.from(context).inflate(R.layout.custom_view, this,true);
		mTextView=(TextView) view.findViewById(R.id.custom_textview);
		mImageView=(ImageView) view.findViewById(R.id.custom_imageview);
	}
	
	public void setText(String text){
		mTextView.setText(text);
	}
	
	public void setTextColor(int color){
		mTextView.setTextColor(color);
	}
	
	public void setTextSize(float size){
		mTextView.setTextSize(size);
	}
	
	public void setImagViewResouce(Drawable drawable){
		mImageView.setImageDrawable(drawable);
	}
	
}

然后是自定义控件的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:gravity="center">
    
    <ImageView
        android:id="@+id/custom_imageview" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>
    
    <TextView 
        android:id="@+id/custom_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="嘿!"/>

</LinearLayout>

最后是对自定义控件的使用

<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"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        android:layout_alignParentBottom="true" >

        <com.sdufe.thea.guo.view.CustomView
            android:id="@+id/custom"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

        <com.sdufe.thea.guo.view.CustomView
            android:id="@+id/custom1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

        <com.sdufe.thea.guo.view.CustomView
            android:id="@+id/custom2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

        <com.sdufe.thea.guo.view.CustomView
            android:id="@+id/custom3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    </LinearLayout>

</RelativeLayout>

package com.sdufe.thea.guo;

import com.sdufe.thea.guo.view.CustomView;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private CustomView mCustomView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		
		initView();
	}

	private void initView() {
		mCustomView=(CustomView) findViewById(R.id.custom);
		mCustomView.setText("我是大坏蛋");
		mCustomView.setTextColor(getResources().getColor(R.color.text_color));
		mCustomView.setImagViewResouce(getResources().getDrawable(R.drawable.phone));
		
		mCustomView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), "别点我", Toast.LENGTH_LONG).show();
			}
		});
	}

}

以上就是全部代码了,这里讲一下我遇到的问题,一般我在填充布局的时候都用LayoutInflater.from(context).inflate(resource, root),可是自定义时,它就是显示不出来,本着程序员精神,就使劲去查,查了查源码,换成LayoutInflater.from(context).inflate(R.layout.custom_view, this,true);就可以了,恩,成功显示出来了,挺开心,顺便看了看源码

一般LayoutInflater.from(context).inflate(resource, root),第一个参数就是要加载的布局id,第二个参数是指给该布局的外部再嵌套一层父布局,如果不需要就直接传null,但是这里要用到他的属性,明显不满足,那就只能用有三个参数的了,那就来说一说三个参数的LayoutInflater.from(context).inflate(R.layout.custom_view, this,true);第一个参数还是加载的布局id,第二个参数是指给该布局的外部再嵌套一层父布局,第三个官方说attachToRoot Whether the inflated hierarchy should be attached to    the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.中文意思就是attachToRoot无论充气层次应附加到根参数?如果为假,根仅用于创建的LayoutParams正确的子类中的XML根视图.

public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
        if (DEBUG) System.out.println("INFLATING from resource: " + resource);
        XmlResourceParser parser = getContext().getResources().getLayout(resource);
        try {
            return inflate(parser, root, attachToRoot);
        } finally {
            parser.close();
        }
    }
当继续追踪第五行代码,你就会知道其实他的实质是利用XML中的Pull解析,这里先暂时放一放,只是说明一下这三个参数

1.当root为空时,attactRoot无效

2.当root不为空时,attachRoot为TRUE会加载布局文件的在外层root

3.当root不为空,attachroot为FALSE时,不会加载最外层的布局文件

ok,就先介绍这些,本博客主要是从Java代码中实现自定义


因篇幅问题不能全部显示,请点此查看更多更全内容

Top