android 自定义组合控件

转载请注明:

http://blog.csdn.net/zenmela2011/article/details/42461229

所谓组合控件,就是把android自带的一些控件组合起来,拼成我们自己需要的一个控件。

本文中我们把ImageView和TextView组合起来,拼装在水平的LinearLayout中,一般的ListView中常见这种布局,效果图如下:


自定义组合控件的步骤:

首先,新建一个布局文件,把组合控件内部的结构整理出来,myview_layout.xml代码如下:

<?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="horizontal" >

    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="TextView" />

</LinearLayout>
在这个布局中,实现了左侧图片,右侧文字的结构。利用这个布局就可以创建控件了。
第二步,新建MyView类,继承LinearLayout,导入 myview_layout.xml布局,并且设置相应的方法,这样 ,这个自定义组合控件的属性在代码中就可以进行设置了。MyView.class代码如下:

package com.fang.androidtest;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 创建组合控件
 * 
 * @author fangfang
 *
 */
public class MyView extends LinearLayout {
	private ImageView mImage;//左侧的图片
	private TextView mText;//右侧的文字

	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		//导入布局文件
		LayoutInflater.from(context).inflate(R.layout.myview_layout, this);
		
		mImage=(ImageView) findViewById(R.id.iv_image);
		mText=(TextView) findViewById(R.id.tv_text);
	}
	
	/**
	 * 设置图片资源
	 * 
	 * @param resourceId 图片ID
	 */
	public void setImageResource(int resourceId){
		mImage.setImageResource(resourceId);
	}
	
	/**
	 * 设置文字
	 * 
	 * @param text 需要显示的文字
	 */
	public void setText(String text){
		mText.setText(text);
	}
}
第三步,在需要改控件的xml布局中直接使用即可。main.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.fang.androidtest.MyView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" />

</RelativeLayout>
最后,在代码中设置该控件的属性。MainActivity.class代码如下:

package com.fang.androidtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

/**
 * 
 * @author fangfang
 * 
 */
public class MainActivity extends Activity{
	private MyView myView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		
		myView=(MyView)findViewById(R.id.myview);
		//设置组合控件的属性
		myView.setImageResource(R.drawable.man);
		myView.setText("人物设置");
	}
}
这样,这个组合控件就彻底完成了。效果如下:


其实在自定义控件中,组合自定义控件应该是最简单的了。一共4步,我们可以举一反三,组合出任何我们需要的控件来。

完整代码下载:

http://download.csdn.net/detail/zenmela2011/8333299

  • 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、付费专栏及课程。

余额充值