自定義頂部標題欄的view

原文網址:點擊打開鏈接
此文在原作者的基礎上加了些註釋,也減少了一些解釋,做了些修改。如果看不太明白的朋友,可以參考原文
1.首先自定義控件屬性
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TopBar">
        <attr name="leftText" format="string" />
        <attr name="leftTextColor" format="color" />
        <attr name="leftTextSize" format="dimension" />
        <attr name="leftTextBackground" format="reference|color" />

        <attr name="centreText" format="string" />
        <attr name="centreTextColor" format="color" />
        <attr name="centreTextSize" format="dimension" />
        <attr name="centreTextBackground" format="reference|color" />

        <attr name="rightText" format="string" />
        <attr name="rightTextColor" format="color" />
        <attr name="rightTextSize" format="dimension" />
        <attr name="rightTextBackground" format="reference|color" />
    </declare-styleable>
</resources></span>


2.自定義view,獲取自定義view的屬性,然後添加點擊事件和給其內的view賦值
<span style="font-size:14px;">package zdd.huangfusheng.topbar;

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.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import zdd.huangfusheng.R;

/**
* Created by ZDD on 2016/7/26.
* 自定義頭
*/
public class TopBarView extends RelativeLayout {

    // 頭部文件的子控件
    private TextView leftTextView, centreTextView, rightTextView;

    // 子控件的位置
    private LayoutParams leftParams, centreParams, rightParams;

    // 左右的點擊事件
    private TopBarListener listener;

    // 左邊的數據
    private String leftText;
    private float leftTextSize;
    private int leftTextColor;
    private Drawable leftTextBackground;

    // 中間的數據
    private String centreText;
    private float centreTextSize;
    private int centreTextColor;
    private Drawable centreTextBackground;

    // 右邊的數據
    private String rightText;
    private float rightTextSize;
    private int rightTextColor;
    private Drawable rightTextBackground;

    public TopBarView(Context context) {
        super(context);
    }

    public TopBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
        leftText = typedArray.getString(R.styleable.TopBar_leftText);
        leftTextSize = typedArray.getDimension(R.styleable.TopBar_leftTextSize, 0);
        leftTextColor = typedArray.getColor(R.styleable.TopBar_leftTextColor, 0);
        leftTextBackground = typedArray.getDrawable(R.styleable.TopBar_leftTextBackground);

        centreText = typedArray.getString(R.styleable.TopBar_centreText);
        centreTextSize = typedArray.getDimension(R.styleable.TopBar_centreTextSize, 0);
        centreTextColor = typedArray.getColor(R.styleable.TopBar_centreTextColor, 0);
        centreTextBackground = typedArray.getDrawable(R.styleable.TopBar_centreTextBackground);

        rightText = typedArray.getString(R.styleable.TopBar_rightText);
        rightTextSize = typedArray.getDimension(R.styleable.TopBar_rightTextSize, 0);
        rightTextColor = typedArray.getColor(R.styleable.TopBar_rightTextColor, 0);
        rightTextBackground = typedArray.getDrawable(R.styleable.TopBar_rightTextBackground);
        typedArray.recycle();
        initView(context);
        setTextViewLocation();
        setTextViewContent();
        setListener();
    }

    public TopBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setText(String leftName) {

        leftTextView.setText(leftName);
        invalidate();
    }

    /**
     * 初始化內部控件
     *
     * @param context
     */
    private void initView(Context context) {
        leftTextView = new TextView(context);
        centreTextView = new TextView(context);
        rightTextView = new TextView(context);
    }

    /**
     * 設置內部控件位置
     */
    private void setTextViewLocation() {
        leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
        leftTextView.setGravity(Gravity.CENTER_VERTICAL);
        addView(leftTextView, leftParams);

        centreParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        centreParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
        centreTextView.setGravity(Gravity.CENTER);
        addView(centreTextView, centreParams);

        rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
        rightTextView.setGravity(Gravity.CENTER);
        addView(rightTextView, rightParams);
    }

    /**
     * 添加數據
     */
    private void setTextViewContent() {
        if (leftText != null && !leftText.equals(""))
            leftTextView.setText(leftText);
        else
            leftTextView.setText("");
        leftTextView.setTextColor(leftTextColor);
        leftTextView.setTextSize(leftTextSize);
        leftTextView.setBackground(leftTextBackground);

        if (centreText != null && !centreText.equals(""))
            centreTextView.setText(centreText);
        else
            centreTextView.setText("");
        centreTextView.setTextColor(centreTextColor);
        centreTextView.setTextSize(centreTextSize);
        centreTextView.setBackground(centreTextBackground);

        if (rightText != null && !rightText.equals(""))
            rightTextView.setText(rightText);
        else
            rightTextView.setText("");
        rightTextView.setTextColor(rightTextColor);
        rightTextView.setTextSize(rightTextSize);
        rightTextView.setBackground(rightTextBackground);
        setBackgroundColor(0xFFF59563);
    }

    public interface TopBarListener {
        void leftClick(View view);

        void centreClick(View view);

        void rightClick(View view);
    }

    public void setOnClickListener(TopBarListener listener) {
        this.listener = listener;
    }

    /**
     * 添加控件點擊事件
     */
    private void setListener() {
        leftTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.leftClick(v);
            }
        });
        centreTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.centreClick(v);
            }
        });
        rightTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.rightClick(v);
            }
        });
    }

    public void setLeftVisible(boolean flag) {
        if (flag) {
            leftTextView.setVisibility(View.VISIBLE);
        } else {
            leftTextView.setVisibility(View.GONE);
        }
    }
}</span>


3.創建activity
<span style="font-size:14px;">package zdd.huangfusheng;

import android.content.Context;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Toast;

import zdd.huangfusheng.topbar.TopBarView;

public class MainActivity extends AppCompatActivity {
    private TopBarView topBarView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        topBarView = (TopBarView) findViewById(R.id.topBarView);

        topBarView.setOnClickListener(new TopBarView.TopBarListener() {
            @Override
            public void leftClick(View view) {
                Toast.makeText(MainActivity.this, "我是左邊", Toast.LENGTH_LONG).show();
            }

            @Override
            public void centreClick(View view) {
                topBarView.setText("woshizuobian");
//                topBarView.setLeftVisible(true);
            }

            @Override
            public void rightClick(View view) {
                topBarView.setText("woshizuobian");
                Toast.makeText(MainActivity.this, "我是右邊邊", Toast.LENGTH_LONG).show();
//                topBarView.setLeftVisible(false);
            }
        });
//        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//
//        String deviceId = telephonyManager.getDeviceId();
//        String androidId = Settings.System.getString(MainActivity.this.getContentResolver(), Settings.System.ANDROID_ID);
//        Toast.makeText(MainActivity.this, "deviceId = " + deviceId + "   androidID = " + androidId, Toast.LENGTH_LONG).show();

    }
}</span>

4.創建activity的xml並且使用自定義的view。並且賦值
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="zdd.huangfusheng.MainActivity">

    <zdd.huangfusheng.topbar.TopBarView
        android:id="@+id/topBarView"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        app:centreText="我是中間"
        app:centreTextColor="@color/colorAccent"
        app:centreTextSize="6sp"

        app:leftTextBackground="@mipmap/ic_launcher"
        app:leftTextColor="@color/colorPrimary"
        app:leftTextSize="7sp"

        app:rightText="我是右邊"
        />
</RelativeLayout></span>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章