关闭

自定义控件实战-Android UI模板设计(文末小彩蛋)

185人阅读 评论(0) 收藏 举报
分类:

先简单说一下实现方向:



接下来我们一步步实现方向:

1.设计所需属性:

在value目录下新建attrs.xml,属性名及类型,

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TopBar">
        <attr name="title" format="string"/>
        <attr name="titleTextSize" format="dimension"/>
        <attr name="titleTextColor" format="color"/>

        <attr name="leftBackground" format="reference|color"/>
        <attr name="leftText" format="string"/>
        <attr name="leftTextColor" format="color"/>

        <attr name="rightBackground" format="reference|color"/>
        <attr name="rightText" format="string"/>
        <attr name="rightTextColor" format="color"/>

    </declare-styleable>
</resources>


2.实现我们的View:

新建类继承RelativeLayout,重写构造方法,并在构造方法里进行映射,对应的属性赋值:

package com.quan.car.topbar;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by 权兴权意 on 2016/8/29.
 */
public class TopBar extends RelativeLayout{

    private Button leftButton,rightButton;
    private TextView middleTitle;

    private int rightTextColor;
    private Drawable rightBackground;
    private String rightText;

    private int leftTextColor;
    private Drawable leftBackground;
    private String leftText;

    private float titleTextSize;
    private int titleTextColor;
    private String title;

    private LayoutParams leftParams,rightParams,titleParams;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        //自定义属性值映射
        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);
        titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize,0);
        titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor,0);
        title = ta.getString(R.styleable.TopBar_title);

        ta.recycle();//回收,节约资源,避免错误;

        leftButton = new Button(context);
        rightButton = new Button(context);
        middleTitle = new TextView(context);

        leftButton.setTextColor(leftTextColor);
        leftButton.setBackground(leftBackground);
        leftButton.setText(leftText);

        rightButton.setTextColor(rightTextColor);
        rightButton.setBackground(rightBackground);
        rightButton.setText(rightText);

        middleTitle.setTextColor(titleTextColor);
        middleTitle.setTextSize(titleTextSize);
        middleTitle.setText(title);
        middleTitle.setGravity(Gravity.CENTER);

        setBackgroundColor(0xFFF59563);

        leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
        addView(leftButton,leftParams);

        rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        addView(rightButton,rightParams);

        titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
        addView(middleTitle,titleParams);
    }
}

3.引用View并模块化开发定义接口设置回调事件,

引用自定义控件:

定义命名空间:

    xmlns:custom="http://schemas.android.com/apk/res-auto"
引用控件并设置属性:

    <com.quan.car.topbar.TopBar
        android:id="@+id/topbar_tb_main"
        android:layout_height="40dp"
        android:layout_width="match_parent"
        custom:leftText="Back"
        custom:leftTextColor="#FFFFFF"
        custom:rightText="More"
        custom:rightTextColor="#FFFFFF"
        custom:title="自定义标题"
        custom:titleTextColor="#123412"
        custom:titleTextSize="10sp"
        ></com.quan.car.topbar.TopBar>

接口相关:

在TopBar.java中进行定义:

    private topbarClickListener listener;

    //定义接口
    public interface topbarClickListener{
        public void leftClick();
        public void rightClick();
    }

    //暴露方法
    public void setOnTopBarClickListener(topbarClickListener listener){
        this.listener = listener;
    }

leftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                //Toast.makeText(context,"Left.",Toast.LENGTH_SHORT).show();
                listener.leftClick();
            }
        });
```
        rightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                //Toast.makeText(context,"Right.",Toast.LENGTH_SHORT).show();
                listener.rightClick();
            }
        });

在MainActivity.java中重写方法,进行注册:

        topBar.setOnTopBarClickListener(new TopBar.topbarClickListener() {
            @Override
            public void leftClick() {
                Toast.makeText(MainActivity.this,"Left.",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void rightClick() {
                Toast.makeText(MainActivity.this,"Right.",Toast.LENGTH_SHORT).show();
            }
        });


核心源代码:

Java:

package com.quan.car.topbar;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by 权兴权意 on 2016/8/29.
 */
public class TopBar extends RelativeLayout{

    private Button leftButton,rightButton;
    private TextView middleTitle;

    private int rightTextColor;
    private Drawable rightBackground;
    private String rightText;

    private int leftTextColor;
    private Drawable leftBackground;
    private String leftText;

    private float titleTextSize;
    private int titleTextColor;
    private String title;

    private LayoutParams leftParams,rightParams,titleParams;

    private topbarClickListener listener;

    //定义接口
    public interface topbarClickListener{
        public void leftClick();
        public void rightClick();
    }

    //暴露方法
    public void setOnTopBarClickListener(topbarClickListener listener){
        this.listener = listener;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public TopBar(final Context context, AttributeSet attrs) {
        super(context, attrs);
        //自定义属性值映射
        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);
        titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize,0);
        titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor,0);
        title = ta.getString(R.styleable.TopBar_title);

        ta.recycle();//回收,节约资源,避免错误;

        leftButton = new Button(context);
        rightButton = new Button(context);
        middleTitle = new TextView(context);

        leftButton.setTextColor(leftTextColor);
        leftButton.setBackground(leftBackground);
        leftButton.setText(leftText);

        rightButton.setTextColor(rightTextColor);
        rightButton.setBackground(rightBackground);
        rightButton.setText(rightText);

        middleTitle.setTextColor(titleTextColor);
        middleTitle.setTextSize(titleTextSize);
        middleTitle.setText(title);
        middleTitle.setGravity(Gravity.CENTER);

        setBackgroundColor(0xFFF59563);

        leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
        addView(leftButton,leftParams);

        rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        addView(rightButton,rightParams);

        titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
        addView(middleTitle,titleParams);

        leftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                //Toast.makeText(context,"Left.",Toast.LENGTH_SHORT).show();
                listener.leftClick();
            }
        });

        rightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                //Toast.makeText(context,"Right.",Toast.LENGTH_SHORT).show();
                listener.rightClick();
            }
        });
    }

    public void setLeftIsVisiable(boolean flag){
        if(flag){
            leftButton.setVisibility(View.VISIBLE);
        }else{
            leftButton.setVisibility(View.INVISIBLE);
        }
    }
}

xml:

<?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.quan.car.topbar.MainActivity">

    <com.quan.car.topbar.TopBar
        android:id="@+id/topbar_tb_main"
        android:layout_height="40dp"
        android:layout_width="match_parent"
        custom:leftText="Back"
        custom:leftTextColor="#FFFFFF"
        custom:rightText="More"
        custom:rightTextColor="#FFFFFF"
        custom:title="自定义标题"
        custom:titleTextColor="#123412"
        custom:titleTextSize="10sp"
        ></com.quan.car.topbar.TopBar>

</RelativeLayout>

文末小彩蛋:

窗口切换键:alt+左/右

代码折叠/展开:ctrl+“+”/“-”

查找对应括号:ctrl+“[” / “]”

Surround With:ctrl+alt+t






0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:78106次
    • 积分:2730
    • 等级:
    • 排名:第13229名
    • 原创:167篇
    • 转载:223篇
    • 译文:2篇
    • 评论:11条