Toolbar自定义及自定义控件类的三个构造函数解析
来源:互联网 发布:二次视频解析接口源码 编辑:程序博客网 时间:2024/05/16 04:40
在Android开发过程中,往往需要使用toolbar作为标题栏。根据不同情况,需要使用不同的toolbar,这个时候就需要我们来自定义toolbar了。
自定义控件中的一种常用方法是new一个类继承你要自定义的控件类,然后就使用你的类名代替原控件。
public class MToobar extends Toolbar{ private View view=null; private Button bumen; public MToobar(Context context) { super(context);Log.d("Dddddd","tttttt1"); } public MToobar(Context context, AttributeSet attrs) { super(context, attrs);Log.d("Dddddd","tttttt2"); } public MToobar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); Log.d("Dddddd","tttttt3"); }
values里面要加一个attrs.xml,里面用来写自定义控件名及属性
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MToobar"> <!--此处写自定义属性--> </declare-styleable></resources>
然后写好自定义的布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/bumen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|left" android:drawableLeft="@drawable/left" android:background="#00000000" android:text="按钮" android:textSize="18sp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" android:layout_centerHorizontal="true" android:text="文字" android:textSize="16sp"/></RelativeLayout>
自定义类的构造函数中里面引用这个布局。(具体哪个构造函数,下面解释)
if(view==null){ view= LayoutInflater.from(getContext()).inflate(R.layout.toobar_content,null); bumen=(Button)view.findViewById(R.id.bumen); ViewGroup.LayoutParams layoutParams=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL); addView(view,layoutParams); }
自定义就完成了。然后就可以使用自定义的这个控件类了。
<com.example.shixun.MToobar android:id="@+id/mtoobar" android:layout_width="match_parent" android:layout_height="60dp" android:layout_gravity="top" android:background="#3ef4ff" android:clickable="true"> </com.example.shixun.MToobar>
private MToobar mToobar=null;mToobar=(MToobar)findViewById(R.id.mtoobar);
到这里还有一个问题没有解决。就是写自定义toolbar类的时候。必须重新构造函数而且还是三个。那么问题是,这三个构造函数分别在什么时候调用呢。下面我来解释一下。
public class MToobar extends Toolbar{ private View view=null; private Button bumen; public MToobar(Context context) { super(context);Log.d("Dddddd","tttttt1"); } public MToobar(Context context, AttributeSet attrs) { super(context, attrs);Log.d("Dddddd","tttttt2"); } public MToobar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); Log.d("Dddddd","tttttt3"); }
第一个构造函数是在你new一个对象的时候调用,
第二个构造函数是在你在xml文件里面声明这个控件时调用,(本例就是调用了第二个)
第三个系统不会自动调用,需要在自己的构造函数中主动调用。
阅读全文
1 0
- Toolbar自定义及自定义控件类的三个构造函数解析
- Android自定义控件三个构造函数
- Android自定义控件三个构造函数
- 自定义控件的三个构造方法含义
- android自定义控件,三个父类构造方法的区别
- Android自定义view的三个构造函数理解
- android自定义控件继承View,其中父类的三个构造方法有什么区别?
- 自定义工具栏 (ToolBar) 控件的外观
- 自定义view(自定义view的时候,三个构造函数各自的作用)
- 自定义view(自定义view的时候,三个构造函数各自的作用)
- 自定义View的三个构造方法
- 自定义控件的构造方法及相关属性的理解
- Android中自定义控件中构造函数的含义
- 编写自定义控件构造函数的正确姿势 - defStyleAttr/defStyleRes
- Swift自定义类的构造函数
- android自定义控件,其三个父类构造方法有什么区别
- easyui的datagrid控件toolbar添加自定义控件
- 自定义View的相关问题(1)--三个构造函数的应用
- 文件下载/导出
- ActiveMQ错误:Wire format negotiation timeout: peer did not send his wire format
- navicat 10.1.7 注册码
- sklearn库之各分类算法简单应用
- Python os 和 os.path模块详解
- Toolbar自定义及自定义控件类的三个构造函数解析
- idea创建自己的archetype
- android-viewpager轮播图遇到的问题
- C++复制构造函数的实现
- python爬虫抓取多关键词搜索的百度图片
- js刷新页面方法大全
- shell基本知识
- Unity 玻璃shader
- PHY芯片直连和加隔离变压器的设计原理的一些资料的收集。