自定义控件例如LinearLayout 的三种方法

本文介绍了自定义LinearLayout的三种方法:1)通过LayoutInflater加载布局并动态创建子元素;2)在自定义控件中声明子元素,并在onFinishInflate中匹配;3)完全在代码中动态生成子视图。每种方法都有其适用场景和特点。
摘要由CSDN通过智能技术生成
最近通过看别人代码和网上搜索,发现现在自定义LinearLayout的方式有三种。
第一种是在扩展的LinearLayout构造函数中使用Inflater加载一个布局,并从中提取出相关的UI组件进行封装,形成一个独立的控件。在使用该控件时,由于它所有的子元素都是在运行时通过代码动态创建的,所以该控件只能以一个独立控件的形式在Layout文件中声明,例如:
public class CustomLayout extends LinearLayout{

       public  CustomLayout(Context context){
                 LayoutInflater mInflater = LayoutInflater.from(context);
                View myView = mInflater.inflate(R.layout.receive, null);
                addView(myView);

       }

}
< LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  androidundefinedrientation="vertical" >
  <LinearLayout 
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          androidundefinedrientation="horizontal">
          <TextView
                   android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值