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代码中实现自定义


代码下载地址:http://download.csdn.net/detail/elinavampire/8141963

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android自定义控件组合是指通过将现有的多个控件组合起来,形成一个新的自定义控件,以实现特定的功能或界面效果。通过组合现有的控件,我们可以更灵活地满足项目需求,并减少重复编写代码的工作量。 在Android中,我们可以使用布局文件XML来定义自定义控件组合。首先,我们需要创建一个新的布局文件,其中包含多个现有的控件。然后,我们可以通过在Java代码中引用这个布局文件,并对其中的控件进行操作和设置属性。 以下是一个简单的示例,演示如何创建一个自定义控件组合: 1. 创建一个新的布局文件,例如"custom_view.xml",在该文件中添加需要组合的多个控件,如TextView、Button等。例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" /> </LinearLayout> ``` 2. 在Java代码中引用该布局文件,并进行相应的操作。例如,在一个Activity中,我们可以通过setContentView方法将该布局文件设置为当前Activity的布局。 ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.custom_view); // 对自定义控件进行操作 TextView textView = findViewById(R.id.textView); Button button = findViewById(R.id.button); // 设置监听器等其他操作... } } ``` 通过上述步骤,我们就可以将多个现有的控件组合成一个新的自定义控件,实现特定的功能或界面效果。当然,在实际应用中,可能还需要对组合控件进行进一步的自定义和功能扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值