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

组合控件的用法:

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


[html]  view plain copy 在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 plain copy 在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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android定义组合是指通过将现有的多个组合起来,形成一个新的自定义件,以实现特定的功能或界面效果。通过组合现有的件,我们可以更灵活地满足项目需求,并减少重复编写代码的工作量。 在Android中,我们可以使用布局文件XML来定义定义件的组合。首先,我们需要创建一个新的布局文件,其中包含多个现有的件。然后,我们可以通过在Java代码中引用这个布局文件,并对其中的件进行操作和设置属性。 以下是一个简单的示例,演示如何创建一个自定义组合: 1. 创建一个新的布局文件,例如"custom_view.xml",在该文件中添加需要组合的多个件,如TextView、Button等。例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" /> </LinearLayout> ``` 2. 在Java代码中引用该布局文件,并进行相应的操作。例如,在一个Activity中,我们可以通过setContentView方法将该布局文件设置为当前Activity的布局。 ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.custom_view); // 对自定义件进行操作 TextView textView = findViewById(R.id.textView); Button button = findViewById(R.id.button); // 设置监听器等其他操作... } } ``` 通过上述步骤,我们就可以将多个现有的组合成一个新的自定义件,实现特定的功能或界面效果。当然,在实际应用中,可能还需要对组合件进行进一步的自定义和功能扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值