自定义组件(三)

来源:互联网 发布:追风打印软件 编辑:程序博客网 时间:2024/05/21 20:46

本节课的目标:创建布局的自定义属性
这里写图片描述
1.在values下创建attr

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="CompoundView">        <attr  name="max" format="integer"/>        <attr name="progress" format="integer" />    </declare-styleable></resources>

CompoundView就是我们styleable 的name
max,progress是我们定义的属性

2.在布局CompoundView的构造方法中添加TypedArray对象
绑定自定义属性

   public CompoundView(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);        TypedArray  ta=context.obtainStyledAttributes(attrs,R.styleable.CompoundView);        max=  ta.getInt(R.styleable.CompoundView_max,100);        progress=  ta.getInt(R.styleable.CompoundView_progress, 0);        ta.recycle();        initView(context);    }

在引用自定义布局的xml引入自定义属性

  xmlns:app="http://schemas.android.com/apk/res-auto"

可以设置属性的初始值

<com.axnet.viewapp.CompoundView        android:id="@+id/main_in"        android:layout_height="wrap_content"        android:layout_width="match_parent"        layout="@layout/main_item"        app:max="10"        app:progress="2"        />

全部代码
MainActivity

public class MainActivity extends AppCompatActivity {    Button main_btn,main_item_btn;    CompoundView myView;    int i;    boolean b=true;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        initClick();    }    private void initClick() {        //下载按钮        main_btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                b=true;                new Thread(new Runnable() {                    @Override                    public void run() {                        while (b){                            try {                                Thread.sleep(1000);                                runOnUiThread(new Runnable() {                                    @Override                                    public void run() {                                        myView.setProgress(i++);                                    }                                });                            } catch (InterruptedException e) {                                e.printStackTrace();                            }                        }                    }                }).start();            }        });//        myView.setMax(50);        myView.setListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                b=false;                i=0;                myView.setProgress(i);            }        });    }    private void initView() {        main_btn=(Button)findViewById(R.id.main_btn);        myView= (CompoundView) findViewById(R.id.main_in);    }}

MainActivity布局代码

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/activity_main"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity"    >    <!--自定义命名空间-->    <!--xmlns:app="http://schemas.android.com/apk/res-auto"-->    <Button        android:id="@+id/main_btn"        android:text="下载"        android:layout_height="wrap_content"        android:layout_width="match_parent"        />    <com.axnet.viewapp.CompoundView        android:id="@+id/main_in"        android:layout_height="wrap_content"        android:layout_width="match_parent"        layout="@layout/main_item"        app:max="10"        app:progress="2"        /></LinearLayout>

CompoundView 代码

public class CompoundView extends LinearLayout{    private Button btn;    private ProgressBar mProgressBar;    private  int max;//最大进度    private int progress;//当前进度    private OnClickListener listener;    public CompoundView(Context context) {        super(context);        initView(context);    }    public CompoundView(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);        TypedArray  ta=context.obtainStyledAttributes(attrs,R.styleable.CompoundView);        max=  ta.getInt(R.styleable.CompoundView_max,100);        progress=  ta.getInt(R.styleable.CompoundView_progress, 0);        ta.recycle();        initView(context);    }    public CompoundView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initView(context);    }    @Override    protected void onFinishInflate() {        super.onFinishInflate();        //设置进度条默认最大值        mProgressBar.setMax(max);        mProgressBar.setProgress(progress);    }    //提供给使用者设置butter监听的    public void setListener(OnClickListener listener) {        this.listener = listener;        //为butter设置监听        btn.setOnClickListener(listener);    }    public void setMax(int max) {        this.max = max;        mProgressBar.setMax(max);    }    public void setProgress(int progress) {        if(progress>max){            this.progress=max;        }else {            this.progress = progress;        }        mProgressBar.setProgress(progress);    }    private void initView(Context mContext ){        LayoutInflater inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        //将compound_view导入LinearLayout中        inflater.inflate(R.layout.compound_view,this);        btn= (Button) this.findViewById(R.id.view_item_btn);        mProgressBar=(ProgressBar)this.findViewById(R.id.view_pbar);    }}

compound_view 布局代码

<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_width="match_parent"              android:orientation="horizontal"              android:gravity="center"              android:padding="20dp"              android:layout_height="match_parent">    <ProgressBar        android:layout_width="5dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:id="@+id/view_pbar"        style="?android:attr/progressBarStyleHorizontal"        />    <Button        android:layout_width="0dp"        android:layout_height="wrap_content"        android:id="@+id/view_item_btn"        android:layout_weight="1"        android:text="取消下载"        /></merge>

在values下创建啊attr

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="CompoundView">        <attr  name="max" format="integer"/>        <attr name="progress" format="integer" />    </declare-styleable></resources>
原创粉丝点击