Android中自定义View可以分两种,一种为使用系统提供View来拼成一个想要的View(比如在程序中多次出现同样的view,可以共用,相当于封装吧),另一种为自己直接使用Paint画。
本文是属于第一种,大概可分4个步骤,如果不需要自定义标签属性第一步则可跳过。
代码如下:
1. 写一个attrs:(如果不需要自定义属性则不需要)
<declare-styleable name="MyLayoutView">
<attr format="reference" name="tile" />
<attr format="reference" name="tittle" />
<attr format="reference" name="content" />
</declare-styleable>
2. layout布局(my_layout.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/my_tile"
android:layout_marginLeft="15dip"
android:orientation="vertical" >
<TextView
android:id="@+id/my_tittle"
style="@style/T7_2_Subhead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:includeFontPadding="false"
android:text="" />
<TextView
android:id="@+id/my_description"
style="@style/T7_5_Bodycopy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false" />
</LinearLayout>
<TextView
android:id="@+id/my_tile"
android:layout_width="200dip"
android:layout_height="200dip"
android:layout_alignParentRight="true"
android:scaleType="fitXY" />
</RelativeLayout>
</LinearLayout>
3. 编写MyLayoutView:
packge com.a.b;
public class MyLayoutView extends LinearLayout {
private int tile;
private int tittle;
private int content;
private ImageView tileView;
private TextView tittleView;
private TextView contentView;
private View view;
public MyLayoutView(Context context) {
super(context);
}
public MyLayoutView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.MyLayoutView);
tile = array.getResourceId(R.styleable.MyLayoutView_tile, -1);
tittle = array.getResourceId(R.styleable.MyLayoutView_tittle, -1);
content = array.getResourceId(R.styleable.MyLayoutView_content, -1);
LayoutInflater inflater = LayoutInflater.from(context);
this.view = inflater.inflate(R.layout.my_layout, null);
tileView = (ImageView) view.findViewById(R.id.my_tile);
tittleView = (TextView) view.findViewById(R.id.my_tittle);
contentView = (TextView) view.findViewById(R.id.my_description);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
this.initialize();
}
private void initialize() {
tileView.setImageResource(tile);
tittleView.setText(tittle);
contentView.setText(content);
LinearLayout.LayoutParams activityParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
this.removeView(this.view);
this.addView(this.view, activityParams);
}
}
4. 使用:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:my="http://schemas.android.com/apk/res/com.a.b"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.a.b.MyLayoutView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
my:content="@string/my_summary1"
my:tile="@drawablemy_one_image"
my:tittle="@string/my_tittle1" />
</RelativeLayout>
相关推荐
android 自定义view比较综合的例子,涉及到一些复杂的实现效果,有一些基础的人可以参考下。
Android自定义View实现转盘旋转的效果
android 自定义view及自定义属性
android 自定义View界面大合集
我的Android进阶之旅------>Android自定义View来实现解析lrc歌词并同步滚动、上下拖动、缩放歌词的功能 http://blog.csdn.net/ouyang_peng/article/details/50813419 1、实现歌词同步滚动的功能,即歌曲播放到哪句...
主要为大家详细介绍了Android自定义view实现选座功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Android自定义View高仿抖音潜艇大挑战小游戏.zipAndroid自定义View高仿抖音潜艇大挑战小游戏.zipAndroid自定义View高仿抖音潜艇大挑战小游戏.zipAndroid自定义View高仿抖音潜艇大挑战小游戏.zipAndroid自定义View...
仿真书籍翻页效果BookPageView简介:实现了仿真翻页效果,教程完整地描述了翻页原理分析到性能优化的过程教程博客:Android自定义View——从零开始实现书籍翻页效果(一) Android自定义View——从零开始实现书籍...
Android 自定义view-仿新浪微博#话题#插入EditText 博客地址http://blog.csdn.net/u014702653/article/details/52799715
android自定义View之NotePad出鞘记,自定义NotePad,android自定义笔记本
Android自定义View实现开关按钮效果,适用于Android进阶初级开发者参考探讨。博文地址:http://blog.csdn.net/jaynm/article/details/52601935
Android自定义View——拼手气转盘 Android自定义View——拼手气转盘
android自定义View实现SwitchView
本文利用SrcIn实现一个人脸扫描的效果,步骤如下: 1、准备两个人脸照片(一明一暗),暗照片作为背景,亮照片作为src前景显示 2、然后通过SrcIn用一个透明矩形框(Dst)与亮照片(src)取交集即可得出一个亮照片的...
Android自定义View——贝塞尔曲线实现直播点赞效果 Android自定义View——贝塞尔曲线实现直播点赞效果
用自定义View的方式来实现圆形的遥控器菜单。更多关于android自定义的知识可访问:http://blog.sina.com.cn/s/blog_9c4f0e790102vyt3.html
根据鸿洋大神博客所写。自己的学习笔记。自定义View实现验证码。
1:水晶(水滴),波浪球实现步骤详解 2:自定义View 最佳实现实例 3:波浪线绘制原理
Android自定义View之签名板,在控件上用触控写出名字,提取jpg格式文件并保存
Android自定义View之高仿QQ健康 ,谷歌deMaterial Design 风格,自定义界面,比较好用的样式