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),系统会回调该构造方法

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值