自定义控件的使用以及动态添加自定义控件

//自定义控件类
package cn.xuganwen.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyView extends LinearLayout {

    private ImageView image;
    private TextView tv_1;
    private TextView tv_3;
    private TextView tv_2;
    public MyView(Context context){
        super(context);
    }
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.MyImageView);
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.myview, this);
        image=(ImageView)findViewById(R.id.image);
        tv_1=(TextView)findViewById(R.id.tv_1);
        tv_2=(TextView)findViewById(R.id.tv_2);
        tv_3=(TextView)findViewById(R.id.tv_3);
        Drawable drawable=array.getDrawable(R.styleable.MyImageView_Src);
        image.setImageDrawable(drawable);
        CharSequence text1=array.getText(R.styleable.MyImageView_Text1);
        CharSequence text2=array.getText(R.styleable.MyImageView_Text2);
        CharSequence text3=array.getText(R.styleable.MyImageView_Text3);
        tv_1.setText(text1);
        tv_2.setText(text2);
        tv_3.setText(text3);
        array.recycle();
    }
    
    public void setText1(String text){
        tv_1.setText(text);
    }
    public void setText2(String text){
        tv_2.setText(text);
    }
    public void setText3(String text){
        tv_3.setText(text);
    }
    
    public void setImageResource(int resId){
        image.setImageResource(resId);
    }
    
}


//activity界面代码
package cn.xuganwen.myview;
import android.app.Activity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnClickListener {

    private LinearLayout ll;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ll=(LinearLayout)findViewById(R.id.ll);
        btn=(Button)findViewById(R.id.btn);
        btn.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View arg0) {
        MyView myView=new MyView(this,null);
        myView.setImageResource(R.drawable.android_48);
        myView.setText1("1");
        myView.setText2("2");
        myView.setText3("3");
        ll.addView(myView);
    }

}


//activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:myview="http://schemas.android.com/apk/res/cn.xuganwen.myview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ll"
    android:orientation="vertical"
    >
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:onClick="create"
        android:text="增加"
        />
    <cn.xuganwen.myview.MyView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        myview:Src="@drawable/android_48"
        myview:Text1="1"
        myview:Text2="2"
        myview:Text3="3"
        />
</LinearLayout>


//attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="MyImageView">
        <attr name="Text1" format="reference|string"></attr>
        <attr name="Text2" format="reference|string"></attr>
        <attr name="Text3" format="reference|string"></attr>
        <attr name="Src" format="reference"></attr>
    </declare-styleable>

</resources>

//自定义控件布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <ImageView 
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        />
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3"
        android:gravity="center"
        >
        <TextView 
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/tv_1"
            android:gravity="center"
            android:text="1"
            />
        <TextView 
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/tv_2"
            android:gravity="center"
            android:text="2"
            />
        <TextView 
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/tv_3"
            android:gravity="center"
            android:text="3"
            />
    </LinearLayout>

</LinearLayout>


你可能感兴趣的:(自定义控件,动态添加自定义控件)