android自定义组合控件,参考Launcher中FolderIcon构成,欢迎拍砖。
2014-04-19 11:20
302 查看
目的:制作一个自定义的组合控件,该控件由其它控件构成,类似GroupView中的多个view,在使用组合控件的时候,只需要构造一个自定义组合控件就行。
![](http://img.blog.csdn.net/20140419112213468?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3NmMTk4Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
代码:
appitem_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<CustomView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/appicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</CustomView>
CustomView.java
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 CustomView extends LinearLayout {
ImageView img;
TextView tv;
Context mContext;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
// TODO Auto-generated constructor stub
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
// TODO Auto-generated constructor stub
}
public CustomView(Context context) {
super(context);
mContext = context;
// TODO Auto-generated constructor stub
}
public static CustomView fromXml(Context context,int resId,int imgId,String name){
CustomView cv = (CustomView) LayoutInflater.from(context).inflate(resId, null);
cv.img = (ImageView) cv.findViewById(R.id.appicon);
cv.tv = (TextView) cv.findViewById(R.id.appname);
cv.img.setBackgroundResource(imgId);
cv.tv.setText(name);
return cv;
}
}
主Activity代码:
CustomView cview = CustomView.fromXml(getApplicationContext(), R.layout.appitem_view, R.drawable.ic_launcher, "hello world");
setContentView(cview);
代码:
appitem_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<CustomView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/appicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</CustomView>
CustomView.java
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 CustomView extends LinearLayout {
ImageView img;
TextView tv;
Context mContext;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
// TODO Auto-generated constructor stub
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
// TODO Auto-generated constructor stub
}
public CustomView(Context context) {
super(context);
mContext = context;
// TODO Auto-generated constructor stub
}
public static CustomView fromXml(Context context,int resId,int imgId,String name){
CustomView cv = (CustomView) LayoutInflater.from(context).inflate(resId, null);
cv.img = (ImageView) cv.findViewById(R.id.appicon);
cv.tv = (TextView) cv.findViewById(R.id.appname);
cv.img.setBackgroundResource(imgId);
cv.tv.setText(name);
return cv;
}
}
主Activity代码:
CustomView cview = CustomView.fromXml(getApplicationContext(), R.layout.appitem_view, R.drawable.ic_launcher, "hello world");
setContentView(cview);
相关文章推荐
- 实例讲解Android应用中自定义组合控件的方法
- android - 自定义(组合)控件 + 自定义控件外观
- android自定义组合控件
- android 控件 自定义组合控件
- Android自定义控件——自定义组合控件
- Android 自定义组合控件
- Android自定义控件之自定义组合控件(三)
- android UI进阶之自定义组合控件
- Android自定义控件(一) 自定义组合控件
- Android布局设计之ExpandableList绑定XML数据构成级联、item布局页面的控件查找及配置child事件,自定义适配显示内容
- Android 自定义UI组合控件设计方法
- android自定义控件(五) 自定义组合控件
- Android自定义View之组合控件-仿电商app顶部栏
- android - 自定义(组合)控件 + 自定义控件外观
- Android自定义控件之组合控件
- android UI进阶之自定义组合控件之一
- Android自定义UI实战(基础篇1)---组合控件封装
- Android学习自定义View(三)——自绘控件和组合控件
- android自定义组合控件图片轮播+小圆点+点击跳转广告页面
- Android自定义控件之组合控件学习笔记分享