利用组合控件自定义Android控件
来源:互联网 发布:汽车大数据营销案例 编辑:程序博客网 时间:2024/05/16 01:15
组合控件的用法:
第一步:新建一个布局文件,在布局文件中放置好相应的控件,代码如下所示。
代码出处:http://blog.csdn.net/guolin_blog/article/details/17357967
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:background="#ffcb05" >
- <Button
- android:id="@+id/button_left"
- android:layout_width="60dp"
- android:layout_height="40dp"
- android:layout_centerVertical="true"
- android:layout_marginLeft="5dp"
- android:background="@drawable/back_button"
- android:text="Back"
- android:textColor="#fff" />
- <TextView
- android:id="@+id/title_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="This is Title"
第二步:创建一个类继承自FrameLayout,在类的构造函数中用了LayoutInflater的inflate()方法来加载刚刚定义的xml布局文件。关于LayoutInfalter的原理分析可以参考:Android LayoutInflater原理分析,带你一步步深入了解View(一)
参考代码:
代码出处:http://blog.csdn.net/guolin_blog/article/details/17357967
- public class TitleView extends FrameLayout {
- private Button leftButton;
- private TextView titleText;
- public TitleView(Context context, AttributeSet attrs) {
- super(context, attrs);
- LayoutInflater.from(context).inflate(R.layout.title, this);
- titleText = (TextView) findViewById(R.id.title_text);
- leftButton = (Button) findViewById(R.id.button_left);
- leftButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- ((Activity) getContext()).finish();
- }
- });
- }
- public void setTitleText(String text) {
- titleText.setText(text);
- }
- public void setLeftButtonText(String text) {
- leftButton.setText(text);
- }
- public void setLeftButtonListener(OnClickListener l) {
- leftButton.setOnClickListener(l);
- }
- }
第三步:在布局文件中引用自定义的View文件
代码出处:http://blog.csdn.net/guolin_blog/article/details/17357967
[html] view plaincopy
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <com.example.customview.TitleView
- android:id="@+id/title_view"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- </com.example.customview.TitleView>
- </RelativeLayout>
这样就成功将一个标题栏控件引入到布局文件中了,运行一下程序,效果如下图所示:
0 0
- 利用组合控件自定义Android控件
- Android自定义控件--组合控件
- android 控件 自定义组合控件
- Android自定义组合控件
- android自定义组合控件
- Android自定义组合控件
- android组合自定义控件
- android 自定义组合控件
- android 自定义组合控件
- android 自定义组合控件
- Android自定义组合控件
- android自定义组合控件
- Android 自定义组合控件
- android自定义组合控件
- android 自定义组合控件
- android 自定义组合控件
- android 自定义组合控件
- Android自定义组合控件
- iOS的UDID废用以及UUID配合keychain的替换方案实现
- Activity生命周期
- HashMap实现原理
- 页面表格原样导出成Excel
- 线程间的同步机制(1)
- 利用组合控件自定义Android控件
- 静态网页简易生成方法
- 无法把指针变量本身传递给一个函数
- 四十条测试你是不是合格的程序员
- 深度学习
- ffmpeg编译问题总结
- CSS
- SpringMVC 基于注解的Controller @RequestMapping @RequestParam..
- sharepoint 2013 在office 365中 如何嵌入JS代码实现搜索功能