利用组合控件自定义Android控件

来源:互联网 发布:汽车大数据营销案例 编辑:程序博客网 时间:2024/05/16 01:15

组合控件的用法:

第一步:新建一个布局文件,在布局文件中放置好相应的控件,代码如下所示。
代码出处:http://blog.csdn.net/guolin_blog/article/details/17357967


[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="50dp"  
  5.     android:background="#ffcb05" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/button_left"  
  9.         android:layout_width="60dp"  
  10.         android:layout_height="40dp"  
  11.         android:layout_centerVertical="true"  
  12.         android:layout_marginLeft="5dp"  
  13.         android:background="@drawable/back_button"  
  14.         android:text="Back"  
  15.         android:textColor="#fff" />  
  16.   
  17.     <TextView  
  18.         android:id="@+id/title_text"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_centerInParent="true"  
  22.         android:text="This is Title"  

第二步:创建一个类继承自FrameLayout,在类的构造函数中用了LayoutInflater的inflate()方法来加载刚刚定义的xml布局文件。关于LayoutInfalter的原理分析可以参考:Android LayoutInflater原理分析,带你一步步深入了解View(一) 

参考代码:

代码出处:http://blog.csdn.net/guolin_blog/article/details/17357967

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class TitleView extends FrameLayout {  
  2.   
  3.     private Button leftButton;  
  4.   
  5.     private TextView titleText;  
  6.   
  7.     public TitleView(Context context, AttributeSet attrs) {  
  8.         super(context, attrs);  
  9.         LayoutInflater.from(context).inflate(R.layout.title, this);  
  10.         titleText = (TextView) findViewById(R.id.title_text);  
  11.         leftButton = (Button) findViewById(R.id.button_left);  
  12.         leftButton.setOnClickListener(new OnClickListener() {  
  13.             @Override  
  14.             public void onClick(View v) {  
  15.                 ((Activity) getContext()).finish();  
  16.             }  
  17.         });  
  18.     }  
  19.   
  20.     public void setTitleText(String text) {  
  21.         titleText.setText(text);  
  22.     }  
  23.   
  24.     public void setLeftButtonText(String text) {  
  25.         leftButton.setText(text);  
  26.     }  
  27.   
  28.     public void setLeftButtonListener(OnClickListener l) {  
  29.         leftButton.setOnClickListener(l);  
  30.     }  
  31.   
  32. }  

第三步:在布局文件中引用自定义的View文件


代码出处:http://blog.csdn.net/guolin_blog/article/details/17357967

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <com.example.customview.TitleView  
  7.         android:id="@+id/title_view"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content" >  
  10.     </com.example.customview.TitleView>  
  11.   
  12. </RelativeLayout>  

这样就成功将一个标题栏控件引入到布局文件中了,运行一下程序,效果如下图所示:



0 0
原创粉丝点击