AndroidAndroid自定义控件Android拾遗

Android开发总结(三)Android自定义控件之标题栏

2016-12-23  本文已影响2765人  程序猿男神

文/程序员男神

前言

又到了周五,提前预祝各位周末愉快。自己这周在公司也没干什么,也没有多少计划,希望周末调整一下,继续上安卓之路。我们都了解一款App的标题栏基本风格都是一致的,这块实现我们可以抽出来自定义写个控件,方便各个页面的调用。话不多说,开始上路。


我的爱好

一、效果图

效果图

先上效果图,直观明了。

二、实现步骤

1、编写自定义属性文件atts.xml,自定义属性中涉及到的属性有左右两边的button的背景图,中间标题的内容,字体大小,字体颜色。

代码如下:

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

2、编写布局文件layout_topbar.xml,使用相对布局,左边一个button,跟父控件左对齐后外边距为5dp,右边的button也是一样,中间的标题居中显示 。

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/leftButton"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp" />

    <TextView
        android:id="@+id/titleText"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:gravity="center" />

    <Button
        android:id="@+id/rightButton"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp" />
</RelativeLayout>

3、编写自定义控件,继承RelativeLayout,获取自定义属性并给对应的控件赋值 。

代码如下:

package com.example.djj.volleymaster.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.djj.volleymaster.R;

/**
 * 描述:自定义topbar的实现
 * <p>
 * Created by djj on 2016/12/23.
 */

public class TopBar extends RelativeLayout {

    private Button leftButton, rightButton;
    private TextView titleTextView;
    private OnLeftAndRightClickListener listener;//监听点击事件

    //设置监听器
    public void setOnLeftAndRightClickListener(OnLeftAndRightClickListener listener) {
        this.listener = listener;
    }

    //按钮点击接口
    public interface OnLeftAndRightClickListener {
        void OnLeftButtonClick();

        void OnRightButtonClick();
    }
    //设置左边按钮的可见性
    public void setLeftButtonVisibility(boolean flag){
        if (flag)
            leftButton.setVisibility(View.VISIBLE);
        else
            leftButton.setVisibility(View.GONE);
    }

    //设置右边按钮的可见性
    public void setRightButtonVisibility(boolean flag){
        if (flag)
            rightButton.setVisibility(View.VISIBLE);
        else
            rightButton.setVisibility(View.GONE);
    }
    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_topbar, this);
        leftButton = (Button) findViewById(R.id.leftButton);
        rightButton = (Button) findViewById(R.id.rightButton);
        titleTextView = (TextView) findViewById(R.id.titleText);
        leftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.OnLeftButtonClick();//点击回调
                }
            }
        });
        rightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.OnRightButtonClick();//点击回调
                }
            }
        });

        //获得自定义属性并赋值
        TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
        int leftBtnBackground = typeArray.getResourceId(R.styleable.TopBar_leftBackground, 0);
        int rightBtnBackground = typeArray.getResourceId(R.styleable.TopBar_rightBackground, 0);
        String titleText = typeArray.getString(R.styleable.TopBar_titleText);
        float titleTextSize = typeArray.getDimension(R.styleable.TopBar_titleTextSize, 0);
        int titleTextColor = typeArray.getColor(R.styleable.TopBar_titleTextColor, 0x38ad5a);
        //释放资源
        typeArray.recycle();
        leftButton.setBackgroundResource(leftBtnBackground);
        rightButton.setBackgroundResource(rightBtnBackground);
        titleTextView.setText(titleText);
        titleTextView.setTextSize(titleTextSize);
        titleTextView.setTextColor(titleTextColor);
    }
}

4、调用自定义控件,在需要的activity或fragment的xml布局中调用

代码如下:

 <com.example.djj.volleymaster.view.TopBar
        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/material_blue_500"
        app:leftBackground="@drawable/left_button_selector"
        app:rightBackground="@drawable/right_button_selector"
        app:titleText="我的首页"
        app:titleTextColor="#FFF"
        app:titleTextSize="6sp" />

左、右按钮点击变色的设置,右边按钮一样,这里省略

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/back_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/back_normal" />
</selector>

按钮资源文件:


back_pressed.png
back_normal.png
right_pressed.png
right_normal.png

最后在activity或fragment中去实现其功能。

代码如下:

TopBar topBar = (TopBar) findViewById(R.id.topbar);
        topBar.setOnLeftAndRightClickListener(new TopBar.OnLeftAndRightClickListener() {
            @Override
            public void OnLeftButtonClick() {
                finish();//左边按钮实现的功能逻辑
            }

            @Override
            public void OnRightButtonClick() {
//右边按钮实现的功能逻辑
                Toast.makeText(HomeActivity.this, "RightButton", Toast.LENGTH_SHORT).show();
            }
        });

总结

其实这个不是最优的解决办法,现在有很多种方法,有更好的实现方法希望大家可以交流学习。

上一篇下一篇

猜你喜欢

热点阅读