Android自定义view----顶部标题栏2

Android自定义view----顶部标题栏2

 1.效果图

               

2.思路

     1)第一种自定义标题栏的方法:http://blog.csdn.net/yk2fxy/article/details/78338486

     2)第二种实现自定标题栏的方法:继承一个现成的viewgroup,我继承的是RelativeLayout,

如上图所示:使用自定义view的方式完成标题栏的自定义,首先需要分析出来标题栏的控件(取消按钮、标题、完成按钮),然后还需要分析出来控件的属性(文本内容、文本颜色、文本大小、背景颜色等);

通过上面的分析我们去定义一个view的属性文件:

<resources>
    <declare-styleable name="TopTitleBarSelfView">
        <attr name="cancelText" format="string" />
        <attr name="cancelTextColor" format="color" />
        <attr name="cancelTextSize" format="dimension" />
        <attr name="cancelBackground" format="color|reference" />
        <attr name="okText" format="string" />
        <attr name="okTextColor" format="color" />
        <attr name="okTextSize" format="dimension" />
        <attr name="oklBackground" format="color|reference" />
        <attr name="titleText" format="string" />
        <attr name="titleTextColor" format="color" />
        <attr name="titleTextSize" format="dimension" />
        <attr name="titleBackground" format="color|reference" />
    </declare-styleable>
</resources>
然后我们去写一个类继承RelativeLayout,代码如下:

package com.yk.skill.androidskillplatform.selfcreate.toptitlebar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yk.skill.androidskillplatform.R;

/**
 * TODO: document your custom view class.
 */
public class TopTitleBarSelfView extends RelativeLayout {
    private String mCancelText,mTitleText,mOkText; // TODO: use a default from R.string...
    private int mCancelTextColor = Color.RED,mTitleTextColor=Color.BLACK,mOkTextColor=Color.BLUE; // TODO: use a default from R.color...
    private float mCancelTextSize = 26,mTitleTextSize=30,mOkTextSize = 26; // TODO: use a default from R.dimen...
    private Drawable mCancelBackground,mTitleBackground,mOkBackground;
    private Button mCancelBtn,mOkBtn;
    private TextView mTitleTv;
    TitleBarClickListener listener;

    public void setListener(TitleBarClickListener listener) {
        this.listener = listener;
    }

    public TopTitleBarSelfView(Context context) {
        super(context);
        init(null, 0);
    }
    public TopTitleBarSelfView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }
    public TopTitleBarSelfView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }
    private void init(AttributeSet attrs, int defStyle) {
        loadAttributes(attrs, defStyle);
        initChildrenView();
    }
    private void initChildrenView() {
        mCancelBtn = new Button(getContext());
        mOkBtn = new Button(getContext());
        mTitleTv = new TextView(getContext());

        mCancelBtn.setText(mCancelText);
        mCancelBtn.setTextSize(mCancelTextSize);
        mCancelBtn.setTextColor(mCancelTextColor);
        mCancelBtn.setBackground(mCancelBackground);
        RelativeLayout.LayoutParams cancelParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        cancelParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        cancelParams.topMargin = 10;
        addView(mCancelBtn,cancelParams);
        mCancelBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.cancel();
            }
        });

        mOkBtn.setText(mOkText);
        mOkBtn.setTextSize(mOkTextSize);
        mOkBtn.setTextColor(mOkTextColor);
        mOkBtn.setBackground(mOkBackground);
        RelativeLayout.LayoutParams okParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        okParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
        okParams.topMargin = 10;
        okParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
        addView(mOkBtn,okParams);
        mOkBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.ok();
            }
        });

        mTitleTv.setText(mTitleText);
        mTitleTv.setTextSize(mTitleTextSize);
        mTitleTv.setTextColor(mTitleTextColor);
        mTitleTv.setBackground(mTitleBackground);
        LayoutParams titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE);
        titleParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
        addView(mTitleTv,titleParams);

    }

    private void loadAttributes(AttributeSet attrs, int defStyle) {
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.TopTitleBarSelfView, defStyle, 0);

        mCancelText = a.getString(R.styleable.TopTitleBarSelfView_cancelText);
        mCancelTextSize = a.getDimension(R.styleable.TopTitleBarSelfView_cancelTextSize,mCancelTextSize);
        mCancelTextColor = a.getColor(R.styleable.TopTitleBarSelfView_cancelTextColor,mCancelTextColor);
        mCancelBackground = a.getDrawable(R.styleable.TopTitleBarSelfView_cancelBackground);

        mOkText = a.getString(R.styleable.TopTitleBarSelfView_okText);
        mOkTextSize = a.getDimension(R.styleable.TopTitleBarSelfView_okTextSize,mOkTextSize);
        mOkTextColor = a.getColor(R.styleable.TopTitleBarSelfView_okTextColor,mOkTextColor);
        mOkBackground = a.getDrawable(R.styleable.TopTitleBarSelfView_oklBackground);

        mTitleText = a.getString(R.styleable.TopTitleBarSelfView_titleText);
        mTitleTextSize = a.getDimension(R.styleable.TopTitleBarSelfView_titleTextSize,mTitleTextSize);
        mTitleTextColor = a.getColor(R.styleable.TopTitleBarSelfView_titleTextColor,mTitleTextColor);
        mTitleBackground = a.getDrawable(R.styleable.TopTitleBarSelfView_titleBackground);

        a.recycle();
    }
}


xml引用:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.yk.skill.androidskillplatform.selfcreate.toptitlebar.TopTitleBarSelfView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        app:cancelText="取消"
        app:okText="完成"
        app:titleText="标题"
        app:titleTextSize="30sp"/>

</FrameLayout>

activity引用:

package com.yk.skill.androidskillplatform.selfcreate.toptitlebar;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.yk.skill.androidskillplatform.R;

/**
 * Created by Administrator on 2017/10/25.
 */

public class SelfTopTitleBarActivity extends Activity{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.toptitlebar_activity);
       TopTitleBarSelfView ttbsv = (TopTitleBarSelfView) findViewById(R.id.title_bar_topselfview);
        ttbsv.setListener(new TitleBarClickListener() {
            @Override
            public void cancel() {
                toast("取消");
            }

            @Override
            public void ok() {
                toast("完成");
            }
        });
    }
    public void toast(String text){
        Toast.makeText(SelfTopTitleBarActivity.this,text,Toast.LENGTH_SHORT).show();
    }
}




猜你喜欢

转载自blog.csdn.net/yk2fxy/article/details/78341910