一个简单的自定义TopBar
来源:互联网 发布:apache ignite 编辑:程序博客网 时间:2024/06/04 19:06
在看《Android群英传》看到自定义view复合控件,就照着书敲了下。
attrs.xml
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MyTopBar"> <attr name="mtitle" format="string"/> <attr name="mtitleTextSize" format="dimension"/> <attr name="mtitleTextColor" format="color"/> <attr name="leftTextColor" format="color"/> <attr name="leftBackground" format="reference|color"/> <attr name="leftText" format="string"/> <attr name="rightTextColor" format="color"/> <attr name="rightBackground" format="reference|color"/> <attr name="rightText" format="string"/> </declare-styleable></resources>
MyTopBar.java
public class MyTopBar extends RelativeLayout { private TypedArray typedArray; private int titleTextColor; private int leftTextColor; private int rightTextColor; private Drawable leftBackground; private Drawable rightBackground; private String rightText; private String leftText; private String title; private float titleTextSize; private Button mLeftButton; private Button mRightButton; private TextView mTitleView; private LayoutParams mLeftParams; private LayoutParams mRightParams; private LayoutParams mTitlepParams; topbarClickListener listener; public MyTopBar(Context context, AttributeSet attrs) { super(context, attrs); //获取属性 typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTopBar); titleTextColor = typedArray.getColor(R.styleable.MyTopBar_mtitleTextColor, 0); leftTextColor = typedArray.getColor(R.styleable.MyTopBar_leftTextColor, 0); rightTextColor = typedArray.getColor(R.styleable.MyTopBar_rightTextColor, 0); leftBackground = typedArray.getDrawable(R.styleable.MyTopBar_leftBackground); rightBackground = typedArray.getDrawable(R.styleable.MyTopBar_rightBackground); leftText = typedArray.getString(R.styleable.MyTopBar_leftText); rightText = typedArray.getString(R.styleable.MyTopBar_rightText); title = typedArray.getString(R.styleable.MyTopBar_mtitle); titleTextSize = typedArray.getDimension(R.styleable.MyTopBar_mtitleTextSize, 10); typedArray.recycle(); mLeftButton = new Button(context); mRightButton = new Button(context); mTitleView = new TextView(context); mLeftButton.setText(leftText); mLeftButton.setTextColor(leftTextColor); mLeftButton.setBackground(leftBackground); mRightButton.setText(rightText); mRightButton.setTextColor(rightTextColor); mRightButton.setBackground(rightBackground); mTitleView.setText(title); mTitleView.setTextColor(titleTextColor); mTitleView.setTextSize(titleTextSize); mTitleView.setGravity(Gravity.CENTER); mLeftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE); addView(mLeftButton, mLeftParams); mRightParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE); addView(mRightButton, mRightParams); mTitlepParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); mTitlepParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE); addView(mTitleView, mTitlepParams); mLeftButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { listener.leftClick(); } }); mRightButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { listener.rightClick(); } }); } public void setOnTopBarClickListener(topbarClickListener listener) { this.listener = listener; } public void setButtonVisable(int id, boolean flag) { if(flag){ if(id==0){ mLeftButton.setVisibility(View.VISIBLE); }else{ mRightButton.setVisibility(View.VISIBLE); } }else{ if(id==0){ mLeftButton.setVisibility(View.GONE); }else{ mRightButton.setVisibility(View.GONE); } } } public interface topbarClickListener { void leftClick(); void rightClick(); }}
使用activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.example.lklknbnhh.mytopbar.MyTopBar android:background="@color/colorPrimaryDark" android:id="@+id/topbar" android:layout_width="match_parent" android:layout_height="50dp" custom:leftText="返回" custom:leftTextColor="#ffffff" custom:rightText="更多" custom:rightTextColor="#ffffff" custom:rightBackground="@mipmap/ic_launcher" custom:mtitle="我是标题" custom:mtitleTextColor="#ffffff" custom:mtitleTextSize="10sp" /></RelativeLayout>
MainActivity.java
public class MainActivity extends Activity { private MyTopBar topBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); topBar = (MyTopBar)findViewById(R.id.topbar); topBar.setOnTopBarClickListener(new MyTopBar.topbarClickListener() { @Override public void leftClick() { Toast.makeText(getApplicationContext(),"左侧按钮被点击",Toast.LENGTH_SHORT).show(); topBar.setButtonVisable(0, false); topBar.setButtonVisable(1,true); } @Override public void rightClick() { Toast.makeText(getApplicationContext(),"右侧按钮被点击",Toast.LENGTH_SHORT).show(); topBar.setButtonVisable(1,false); topBar.setButtonVisable(0,true); } }); }}
0 0
- 一个简单的自定义TopBar
- 一个自定义的Topbar模板
- Android 自定义控件之简单自定义一个TopBar
- 自定义View-->实现顶部Topbar的简单UI模板
- android 自定义TopBar的使用
- 自定义一个TopBar(完整流程)
- 自定义TopBar
- 自定义TopBar
- 自定义topbar
- 自定义topbar
- 自定义TopBar
- 自定义Topbar
- 自定义TopBar
- 自定义控件——一个topbar的实现(顺便学习接口的回调)
- android自定义控件(TopBar的实现)
- android自定义控件(TopBar的实现)
- Android 自定义TopBar的复用
- 关于android自定义控件Topbar的学习
- 复习redis (一)
- Android 混淆代码总结
- Java并发编程系列之三十:多线程的代价
- python数据分析中出现的奇怪的bug
- The specified bucket is not valid
- 一个简单的自定义TopBar
- Java中使用ibatis调用Oracle存储过程和函数
- SharePoint的创建
- android 读取,清除缓存通用于所有手机 解决空指针
- Unity环境搭建Photon服务器
- Java NIO Scatter / Gather
- SublimeText 3 packagecontrol installation#st3
- 将yuv转成png图
- openfire 成功安装后无法启动,无法访问http://ip:9090