Android View之UI模板

来源:互联网 发布:h5互动游戏 源码 编辑:程序博客网 时间:2024/04/28 19:54
1. 设计需要的属性
values文件夹中创建attrs.xml的文件,在<resource>的tag内添加我们所需要的属性声明
<resources>
<!-- reference 代表drawable对象-->
<declare-styleable name="Topbar">
<attr name="centerTitle" format="string"/>
<attr name="centerTitleTextSize" format="dimension"/>
<attr name="centerTitleTextColor" format="color"/>
<attr name="leftTextColor" format="color"/>
<attr name="leftBackground" format="reference|color"/>
<attr name="leftText" format="string"/>
<attr name="rightTextColor" format="color"/>
<attr name="rightBackground" format="reference|color"/>
<attr name="rightText" format="string"/>
</declare-styleable>
</resources>
若编译时属性有冲突,更改名字
2. 创建一个实现view的类
package com.hc.test;import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.Gravity;import android.view.View;import android.widget.Button;import android.widget.RelativeLayout;import android.widget.TextView;/** * Created by Administrator on 2016/8/22. */public class Topbar extends RelativeLayout {    private Button      leftButton, rightButton;    private TextView    tvTitle;    private int         leftTextColor;    private Drawable    leftBackground;    private String      leftText;    private int         rightTextColor;    private Drawable    rightBackground;    private String      rightText;    private float       centerTitleTextSize;    private int         centerTitleTextColor;    private String      centerTitle;    private LayoutParams leftParams, rightParams;    private LayoutParams titleParams;    // 定义监听事件接口    public interface TopbarClickListener{        void onLeftClick();        void onRightClick();    }    private TopbarClickListener listener;    public void setOnTopbarClickLister(TopbarClickListener listener){        this.listener = listener;    }    public Topbar(Context context, AttributeSet attrs) {        super(context, attrs);        // 该方法获取在xml中定义的属性        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Topbar);        leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);        leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);        leftText = ta.getString(R.styleable.Topbar_leftText);        rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);        rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);        rightText = ta.getString(R.styleable.Topbar_rightText);        centerTitleTextSize = ta.getDimension(R.styleable.Topbar_centerTitleTextSize, 0);        centerTitleTextColor = ta.getColor(R.styleable.Topbar_centerTitleTextColor, 0);        centerTitle = ta.getString(R.styleable.Topbar_centerTitle);        ta.recycle(); //  回收        leftButton = new Button(context);        rightButton = new Button(context);        tvTitle = new TextView(context);        leftButton.setTextColor(leftTextColor);        leftButton.setBackground(leftBackground);        leftButton.setText(leftText);        rightButton.setTextColor(rightTextColor);        rightButton.setBackground(rightBackground);        rightButton.setText(rightText);        tvTitle.setText(centerTitle);        tvTitle.setTextColor(centerTitleTextColor);        tvTitle.setTextSize(centerTitleTextSize);        tvTitle.setGravity(Gravity.CENTER);        setBackgroundColor(0xFFF59563);        // 定义子控件的参数,并添加到父控件中        leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);        addView(leftButton, leftParams);        rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);        addView(rightButton, rightParams);        titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);        addView(tvTitle, titleParams);        // button的事情采用传进来的回调函数        leftButton.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                if(listener != null){                    listener.onLeftClick();                }            }        });        rightButton.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                if(listener != null){                    listener.onRightClick();                }            }        });    }}
3. xml中引用view
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:custom="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.hc.test.MainActivity">    <com.hc.test.Topbar        android:id="@+id/topbar"        android:layout_width="match_parent"        android:layout_height="40dp"        custom:leftBackground="@drawable/round_blue_btn"        custom:leftText="Back"        custom:leftTextColor="#FFFFFF"        custom:rightBackground="@drawable/round_blue_btn"        custom:rightText="GO"        custom:rightTextColor="#FFFFFF"        custom:centerTitle="自定义标题"        custom:centerTitleTextColor="#235622"        custom:centerTitleTextSize="10sp"        >    </com.hc.test.Topbar></RelativeLayout>
注意:
xmlns:xml namespace的意思,告诉我们如何去解析属性
引用第三方名字空间,我们需要加上如下这句
xmlns:custom="http://schemas.android.com/apk/res-auto"
我们定义的属性则都以custom:开头

效果图:


0 0
原创粉丝点击