Android自定义View 自定义组合控件

简介:

自定义组合控件:

以三国杀游戏武将为例,包括武将头像,血条,装备区

1.先定义该组合的XML文件布局

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="wrap_content"
 4     android:layout_height="wrap_content"
 5     android:orientation="horizontal" >
 6     <LinearLayout
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:orientation="vertical"
10         >
11         <ImageView
12             android:id="@+id/touxiang"
13             android:layout_width="80dp"
14             android:layout_height="80dp"
15             android:maxWidth="80dp"
16             android:maxHeight="80dp"    
17             >
18         </ImageView>
19         <ImageView
20             android:id="@+id/blood"
21             android:layout_width="wrap_content"
22         android:layout_height="wrap_content"
23             android:maxWidth="80dp"
24             android:maxHeight="20dp"
25             >            
26         </ImageView>
27         
28     </LinearLayout>
29     
30     <LinearLayout
31         android:layout_width="wrap_content"
32         android:layout_height="wrap_content"
33         android:orientation="vertical"
34         android:layout_gravity="center_vertical"
35         >
36         <TextView
37             android:layout_width="wrap_content"
38             android:layout_height="wrap_content"
39             android:text="武器"
40             ></TextView>
41         <TextView
42             android:layout_width="wrap_content"
43             android:layout_height="wrap_content"
44             android:text="防具"
45             ></TextView>
46         <TextView
47             android:layout_width="wrap_content"
48             android:layout_height="wrap_content"
49             android:text="+1马"
50             ></TextView>
51         <TextView
52             android:layout_width="wrap_content"
53             android:layout_height="wrap_content"
54             android:text="-1马"
55             ></TextView>
56     </LinearLayout>
57 
58 </LinearLayout>
复制代码

2.自定义一个继承布局的类 

复制代码
public class GeneralFrame extends LinearLayout {

    ImageView general;
    ImageView blood;
    TextView wuqi;
    TextView fangju;
    TextView jiayima;
    TextView jianyima;
    public GeneralFrame(Context context) {
        //super(context);
        // TODO Auto-generated constructor stub
        this(context,null);
    }
    public GeneralFrame(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
          //在构造函数中将Xml中定义的布局解析出来。   
        LayoutInflater.from(context).inflate(R.layout.generalframe, this, true);  
        general=(ImageView)findViewById(R.id.touxiang);
        blood=(ImageView)findViewById(R.id.blood);
        blood.setImageResource(R.drawable.blood);
        //wuqi=(TextView)findViewById(R.id
    }
复制代码

可在XML文件里调用该类

 < com.layouts.uitest.GeneralFrame
     android:layout_height="wrap_content"
      android:layout_width="wrap_content"
/>

也可以在代码中动态添加该类

GeneralFrame general=new GeneralFrame(this);
general.setGeneralImage(R.drawable.diaochan);
linear2.addView(general);

自定义View.

自定义类继承View 

复制代码
public class MyView extends View
{
  
   public MyView (Context c,AttributeSet set)
  {
   }
   @Override
   public void onDraw(Canvas canvas)
  {
     
   }  
}
复制代码

调用方法同自定义控件一样。

自定义View的构造方法一定要选中 public MyView (Context c,AttributeSet set),系统会回调该构造方法

相关文章
|
21天前
|
Java API 调度
Android系统 自定义开机广播,禁止后台服务,运行手动安装应用接收开机广播
Android系统 自定义开机广播,禁止后台服务,运行手动安装应用接收开机广播
43 0
|
21天前
|
存储 Java Linux
Android Mstar增加IR 自定义遥控头码完整调试过程
Android Mstar增加IR 自定义遥控头码完整调试过程
29 1
|
1月前
|
缓存 测试技术 Android开发
深入探究Android中的自定义View绘制优化策略
【4月更文挑战第8天】 在Android开发实践中,自定义View的绘制性能至关重要,尤其是当涉及到复杂图形和动画时。本文将探讨几种提高自定义View绘制效率的策略,包括合理使用硬件加速、减少不必要的绘制区域以及利用缓存机制等。这些方法不仅能改善用户体验,还能提升应用的整体性能表现。通过实例分析和性能测试结果,我们将展示如何有效地实现这些优化措施,并为开发者提供实用的技术指南。
|
2天前
|
存储 消息中间件 缓存
Android应用开发:实现自定义View的高效绘制
【5月更文挑战第12天】 在Android开发中,创建高性能的自定义视图是一项挑战,它要求开发者深入理解Android的绘图机制以及UI渲染过程。本文将探讨如何优化自定义View的绘制流程,减少不必要的重绘和布局计算,以提升应用的响应速度和流畅度。我们将介绍几种关键策略,包括利用硬件加速、缓存绘制内容和使用高效的数据结构来存储视图状态。通过实例分析和性能对比,读者将学会如何在自己的应用中运用这些技巧,从而打造出更加流畅和响应迅速的用户界面。
|
4天前
|
XML Android开发 数据格式
Android下自定义Button样式
Android下自定义Button样式
10 3
|
5天前
|
XML Java Android开发
如何美化android程序:自定义ListView背景
如何美化android程序:自定义ListView背景
|
5天前
|
搜索推荐 Android开发
自定义Android标题栏TitleBar布局
自定义Android标题栏TitleBar布局
|
5天前
|
XML Java Android开发
Android控件动态使用 (转)
Android控件动态使用 (转)
|
21天前
|
Android开发 芯片
Android源代码定制:移除无用lunch|新建lunch|自定义customize.mk
Android源代码定制:移除无用lunch|新建lunch|自定义customize.mk
26 3
|
21天前
|
移动开发 Java Unix
Android系统 自动加载自定义JAR文件
Android系统 自动加载自定义JAR文件
45 1