Android(安卓)自定义View:TopBar
16lz
2021-01-24
概述
多种自定义View的范例演示
源码下载
下载地址:https://github.com/zhuanghongji/CustomViewZhj
博客中只是部分演示代码,后面新增演示的代码均只在github上更新。
效果图
代码展示:
MainActivity.java :
import com.zhuanghongji.customviewzhj.view.TopBar;public class MainActivity extends AppCompatActivity { TopBar mTopBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTopBar = (TopBar) findViewById(R.id.topBar);// mTopBar.setLeftButtonIsVisible(false); mTopBar.setOnTopBarClickListener(new TopBar.TopBarClickListener() { @Override public void leftBtnClick() { Toast.makeText(MainActivity.this, " 点击了 leftBtn ", Toast.LENGTH_SHORT).show(); } }); }}activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:zhj="http://schemas.android.com/apk/res-auto" 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=".MainActivity"> <com.zhuanghongji.customviewzhj.view.TopBar android:id="@+id/topBar" android:layout_width="match_parent" android:layout_height="56dp" zhj:leftBtnBackground="#ff0000" zhj:leftBtnText="leftBtn" zhj:leftBtnTextColor="#000000" zhj:titleText="我是自定义的title" zhj:titleTextColor="#000000" zhj:titleTextSize="18sp" zhj:topBarBackground="#00ff00"> </com.zhuanghongji.customviewzhj.view.TopBar></RelativeLayout>TopBar.java :
import com.zhuanghongji.customviewzhj.R;public class TopBar extends RelativeLayout { private Button mLeftBtn; private TextView mTitleTv; // 左Button属性 private int leftBtnTextColor; private Drawable leftBtnBackground; private String leftBtnText; // title属性 private int titleTextColor; private String titleText; private float titleTextSize; // topBar属性 private Drawable topBarBackground; // 布局属性 private LayoutParams leftBtnParams, titleParams; // 点击事件监听器接口 -- public public interface TopBarClickListener { public void leftBtnClick(); } private TopBarClickListener listener; // 设置监听器 public void setOnTopBarClickListener(TopBarClickListener listener) { this.listener = listener; } // 供调用的方法 public void setLeftButtonIsVisible(boolean visible) { if (visible) { mLeftBtn.setVisibility(View.VISIBLE); } else { mLeftBtn.setVisibility(View.GONE); } } // 三个构造方法,均调用均调用三个参数的那个 public TopBar(Context context) { this(context, null); } public TopBar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public TopBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar); // 取自定义属性 -- leftBtn leftBtnText = ta.getString(R.styleable.TopBar_leftBtnText); leftBtnTextColor = ta.getColor(R.styleable.TopBar_leftBtnTextColor, Color.WHITE); leftBtnBackground = ta.getDrawable(R.styleable.TopBar_leftBtnBackground); // 取自定义属性 -- title titleText = ta.getString(R.styleable.TopBar_titleText); titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, Color.WHITE); titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 12); // 取自定义属性 -- topBar topBarBackground = ta.getDrawable(R.styleable.TopBar_topBarBackground); //回收TypeArray(避免浪费资源,避免以为缓存导致的错误) ta.recycle(); mLeftBtn = new Button(context); mTitleTv = new TextView(context); //设置自定义属性 mLeftBtn.setText(leftBtnText); mLeftBtn.setTextColor(leftBtnTextColor); mLeftBtn.setBackground(leftBtnBackground); mTitleTv.setText(titleText); mTitleTv.setTextSize(titleTextSize); mTitleTv.setTextColor(titleTextColor); setBackground(topBarBackground); // 可以通过此代码改变topBar背景色// setBackgroundColor(0x00ff00); // 在这里我们直接设置成绿色 // 设置布局 -- 左Button leftBtnParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); leftBtnParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE); leftBtnParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); addView(mLeftBtn, leftBtnParams); // 设置布局 -- title titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE); addView(mTitleTv, titleParams); mLeftBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { listener.leftBtnClick(); //左Button点击事件 } }); }}attrs.xml :
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="TopBar"> <!--文字--> <attr name="leftBtnText" format="string"/> <attr name="titleText" format="string"/> <!--文字颜色--> <attr name="leftBtnTextColor" format="color"/> <attr name="titleTextColor" format="color"/> <!--文字大小--> <attr name="titleTextSize" format="dimension"/> <!--背景色,因为有时候也会用到数值来表达颜色,所以加上reference格式--> <attr name="leftBtnBackground" format="reference|color" /> <attr name="topBarBackground" format="reference|color" /> </declare-styleable></resources>
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- AndroidICS4.0---->LockScreen锁屏流程【Android源码解析九】
- Android(安卓)Kotlin入门-属性和字段
- Android开发常用代码
- Android(安卓)代码中动态为RadioGroup添加RadioButton
- Facebook发布React Native for Android
- Android本地代码直接访问远程数据库(Rxjava方式)
- ormlite数据库存放sd卡,android卸载应用保留数据的方法
- Android第一行代码学习笔记六----手机多媒体