顶部分类标签

来源:互联网 发布:人肉搜索网络团队 编辑:程序博客网 时间:2024/06/07 03:21

预览图
预览图

1、自定义布局(SortTopView)

package com.jiangyy.demo;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.drawable.Drawable;import android.support.v4.content.ContextCompat;import android.text.TextPaint;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;public class SortTopView extends LinearLayout {    private CallBackListener callBackListener;    private TextView tvLeft;    private TextView tvRight;    private int pressBackground; //按下时的背景    private int unpressBackground; //非按下时的背景    private int pressColor; //按下时的字体颜色    private int unpressColor; //非按下时的字体颜色    private String leftText = "左按钮"; //左按钮的文本内容    private String rightText = "右按钮"; //右按钮的文本内容    public SortTopView(Context context) {        super(context);        init(null, 0);    }    public SortTopView(Context context, AttributeSet attrs) {        super(context, attrs);        init(attrs, 0);    }    public SortTopView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        init(attrs, defStyle);    }    private void init(AttributeSet attrs, int defStyle) {        final TypedArray a = getContext().obtainStyledAttributes(                attrs, R.styleable.SortTopView, defStyle, 0);        pressBackground = a.getResourceId(R.styleable.SortTopView_pressBackground, R.drawable.shape_press);        unpressBackground = a.getResourceId(R.styleable.SortTopView_unpressBackground, R.drawable.shape_unpress);        pressColor = a.getColor(R.styleable.SortTopView_pressTextColor, Color.WHITE);        unpressColor = a.getColor(R.styleable.SortTopView_unpressTextColor, ContextCompat.getColor(getContext(), android.R.color.holo_blue_dark));        leftText = a.getString(R.styleable.SortTopView_leftText);        rightText = a.getString(R.styleable.SortTopView_rightText);        tvLeft = new TextView(getContext());        tvLeft.setText(leftText);        tvLeft.setBackgroundResource(pressBackground);        tvLeft.setPadding(20, 10, 20, 10);        tvLeft.setTextColor(pressColor);        tvLeft.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View view) {                tvLeft.setBackgroundResource(pressBackground);                tvLeft.setTextColor(pressColor);                tvRight.setBackgroundResource(unpressBackground);                tvRight.setTextColor(unpressColor);                if (callBackListener != null) {                    callBackListener.leftClick(view);                }            }        });        addView(tvLeft);        tvRight = new TextView(getContext());        tvRight.setText(rightText);        tvRight.setBackgroundResource(unpressBackground);        tvRight.setPadding(20, 10, 20, 10);        tvRight.setTextColor(unpressColor);        tvRight.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View view) {                tvRight.setBackgroundResource(pressBackground);                tvRight.setTextColor(pressColor);                tvLeft.setBackgroundResource(unpressBackground);                tvLeft.setTextColor(unpressColor);                if (callBackListener != null) {                    callBackListener.rightClick(view);                }            }        });        addView(tvRight);    }    public CallBackListener getCallBackListener() {        return callBackListener;    }    public void setCallBackListener(CallBackListener callBackListener) {        this.callBackListener = callBackListener;    }    public interface CallBackListener {        /**         * 左边按钮点击事件         *         * @param view         */        public void leftClick(View view);        /**         * 右边按钮点击事件         *         * @param view         */        public void rightClick(View view);    }}

2、命名空间(attrs_sort_top_view.xml)

<resources>    <declare-styleable name="SortTopView">        <attr name="pressBackground" format="color|reference" />        <attr name="unpressBackground" format="color|reference" />        <attr name="pressTextColor" format="color" />        <attr name="unpressTextColor" format="color" />        <attr name="leftText" format="string" />        <attr name="rightText" format="string" />    </declare-styleable></resources>

3、资源文件(shape_unpress.xml和shape_press.xml)

shape_press.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <solid android:color="@android:color/holo_blue_dark" />    <stroke        android:width="1dp"        android:color="@android:color/holo_blue_dark" /></shape>

shape_unpress

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <solid android:color="@android:color/white" />    <stroke        android:width="1dp"        android:color="@android:color/holo_blue_dark" /></shape>

4、使用方法

xml文件

<?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"    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.jiangyy.demo.MainActivity">    <com.jiangyy.demo.SortTopView        android:id="@+id/SortTopView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello World!"        app:leftText="左按钮"        app:pressBackground="@drawable/shape_press"        app:pressTextColor="@android:color/white"        app:rightText="右按钮"        app:unpressBackground="@drawable/shape_unpress"        app:unpressTextColor="@android:color/holo_blue_dark" /></RelativeLayout>

Activity文件

package com.jiangyy.demo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        SortTopView sortTopView = (SortTopView) findViewById(R.id.SortTopView);        sortTopView.setCallBackListener(new SortTopView.CallBackListener() {            @Override            public void leftClick(View view) {                Toast.makeText(MainActivity.this, "左边点击", Toast.LENGTH_SHORT).show();            }            @Override            public void rightClick(View view) {                Toast.makeText(MainActivity.this, "右边点击", Toast.LENGTH_SHORT).show();            }        });    }}
0 0
原创粉丝点击