顶部分类标签
来源:互联网 发布:人肉搜索网络团队 编辑:程序博客网 时间: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
- 顶部分类标签
- 标签分类
- ionic 新闻分类停留在顶部
- Struts标签分类
- Struct2标签的分类
- html/xhtml标签分类
- 分类与标签
- html常用标签分类
- 使用bitset分类标签
- HTML标签的分类
- 原html标签分类
- html5标签分类
- Html标签元素分类
- HTML标签分类
- html标签分类
- 输入标签分类demo
- HTML标签的分类
- html标签/分类/特点
- StringUtil工具类
- 几种简单的查找和排序思想
- PAT-B 1043. 输出PATest
- 魔窗-企业级Deeplink解决方案,你的App增长引擎!
- Freemarker判断序列中是否包含某个元素
- 顶部分类标签
- HashMap实现原理
- 管道popen
- 真是汗颜··我这是糟糕透了
- 解决This function or variable may be unsafe
- 面试经验
- java基础教学,从零开始
- Runnable和Thread的区别
- C语言单向链表的建立