Android自定义组合控件

自定义组合控件为自定义控件中的一种,由已有的控件组合而成。自定义控件类需要继承已有控件(RelativeLayout 、LinearLayout等)

1.组合控件的XML布局文件( res/layout/xxx.xml )

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

</RelativeLayout>

2.设置组合控件的属性的XML文件 ( res/value/attrs.xml )

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 声明-风格        自定义控件名-->
    <declare-styleable name="MyView">
        
        <attr name="attrN1" format="string"/>
        <!--   属性名           属性类型     -->
        
        <attr name="attrN2">
            <flag name="FLAG_N1" value="1"/>
            <!--   属性名        属性值(此值只能为int)  -->
            <flag name="FLAG_N2" value="1"/>
        </attr>

        </attr name="attrN3" format="reference"/>
        <!--   属性名           属性值(资源R.~.~)  -->
        
    </declare-styleable>
</resources>

declare-声明  styleable-风格  attr-属性  flag-标识、标志

format="reference"标识类型为资源 ( R.~.~ )

3.创建自定义组合控件类( app / java / 包名 / xxx.java ) 

public class MyView extends RelativeLayout { //继承已有控件(RelativeLayout、LinearLayout等)
    private int i;
    private TextView textView;
    public MyView(Context context) {
        super(context);
        //用于Java文件中的构造函数
    }
    
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //用于XML布局文件中的构造函数
        getAttrs(context,attrs);
        setView(context);
    }
    
    private void getAttrs(Context context,AttributeSet attrs){
        //获取属性值
        TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.MyView);
        
        i=typedArray.getInt(R.styleable.MyView_attrN2,1);
        int id=typedArray.getResourceId(R.styleable.MyView_attrN3,R.~.~);
        //typedArray.getString  typedArray.getBoolean

        //回收
        typedArray.recycle();
    }
    
    private void setView(Context context){
        LayoutInflater.from(context).inflate(R.layout.myview,this);
        
        textView=(TextView) findViewById( ~ );
        textView.setText(i);
    }
}

获取自定义控件属性值以后一定要回收TypedArray

使用getResourceId()获取类型为reference的资源ID

自定义组合控件需要继承已有控件(RelativeLayout、LinearLayout等)

LayoutInflater.from( context ).inflate( R.layout.myview , this ); 根为this

4.使用自定义组合控件

<MyView 
    app:attrN1="testString"
    app:attrN2="FLAGN1"
    ... ...
/>

自定义属性使用:  app: 属性名 = " 属性值 " 

5.注:自定义控件类需实现方法

public class MyView extends RelativeLayout {
    public MyView(Context context) {
        super(context);
        //用于Java文件中的构造函数
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //用于XML布局文件中的构造函数

        TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.MyView);
        LayoutInflater.from(context).inflate(R.layout.myview,this);
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //该方法用于测量尺寸,在该方法中可以设置控件本身或其子控件的宽高
    }
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //该方法用于绘制图像
    }
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        //用于指定布局中子控件的位置
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

在下嗷呜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值