Android自定义组合控件——简单明了

1.自定义属性,可在XML文件中直接使用:

在res下values文件新建 attrs.xml ,然后写你想要的自定义属性,比如

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="CustomNewToolBar">
        <attr name="titleText" format="string|reference" />
        <attr name="myTitleTextColor" format="color|reference" />
        <attr name="titleTextSize" format="dimension|reference" />
        <attr name="leftImageSrc" format="reference" />
        <attr name="rightImageSrc" format="reference" />
    </declare-styleable>

</resources>

2.自定义Java 类(重点):

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CustomNewToolBar extends RelativeLayout {

    private ImageView leftImg,rightImg;
    private TextView mTitle;

    public CustomNewToolBar(Context context) {
        super(context);
    }

    public CustomNewToolBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context,attrs);
    }

    //声明一个接口
    public  interface ImgOnclick{
        void leftOnclick();
        void rightOnclick();
    }
    //声明接口变量
    private ImgOnclick mImgOnclick;

    //为接口变量设置一个set方法
    public void setImgOnclick(ImgOnclick imgOnclick){
        this.mImgOnclick = imgOnclick;
    }


    private void initView(Context context,AttributeSet attrs) {
        View.inflate(context,R.layout.custom_view,this);
        leftImg = findViewById(R.id.img_left);
        rightImg = findViewById(R.id.img_right);
        mTitle = findViewById(R.id.title);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomNewToolBar);
        String titleText = ta.getString(R.styleable.CustomNewToolBar_titleText);
        //第二个参数表示默认颜色
        int titleTextColor = ta.getColor(R.styleable.CustomNewToolBar_myTitleTextColor, Color.BLACK);
        //已经由sp转为px
        float titleTextSize = ta.getDimension(R.styleable.CustomNewToolBar_titleTextSize, 12);

        //读取图片
        Drawable leftDrawable = ta.getDrawable(R.styleable.CustomNewToolBar_leftImageSrc);
        Drawable rightDrawable = ta.getDrawable(R.styleable.CustomNewToolBar_rightImageSrc);

        leftImg.setImageDrawable(leftDrawable);
        rightImg.setImageDrawable(rightDrawable);
        mTitle.setText(titleText);
        mTitle.setTextColor(titleTextColor);
        mTitle.setTextSize(titleTextSize);

        //如果XML中没有加载这个属性,就需要隐藏,否则点击事件同样有效
        if (null == leftDrawable ){
            leftImg.setVisibility(GONE);
        }

        if (null == rightDrawable ){
            rightImg.setVisibility(GONE);
        }

        leftImg.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mImgOnclick){
                    mImgOnclick.leftOnclick();
                }
            }
        });

        rightImg.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mImgOnclick){
                    mImgOnclick.rightOnclick();
                }
            }
        });

    }

    
    public CustomNewToolBar setTitle(String title){
        if (null != mTitle){
            mTitle.setText(title);
        }
        //链式调用的关键
        return this;
    }

    public CustomNewToolBar setTitleSize(float in){
        if (null != mTitle){
            mTitle.setTextSize(in);
        }
        return this;
    }

}

3.在XML中使用你想要的属性

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.cris.customview.CustomNewToolBar
        android:id="@+id/custom"
        app:titleText="2222"
        app:leftImageSrc="@mipmap/left"
        app:rightImageSrc="@mipmap/right"
        app:myTitleTextColor="@color/colorPrimaryDark"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</RelativeLayout>

4.你也可以在代码中设置相关属性,前提是Java代码中有相关方法:

CustomNewToolBar customNewToolBar =findViewById(R.id.custom);
        customNewToolBar.setTitle("标题");
        customNewToolBar.setTitleSize(20);

        customNewToolBar.setImgOnclick(new CustomNewToolBar.ImgOnclick() {
            @Override
            public void leftOnclick() {
                Toast.makeText(MainActivity.this,"点击左边img",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void rightOnclick() {
                Toast.makeText(MainActivity.this,"点击右边img",Toast.LENGTH_SHORT).show();
            }
        });

自定义 View 的几个知识点,要自定义一个控件主要包含几个方法。

onMeasure:测量View的宽高;
onLayout:计算当前View以及子View的位置;​​​​​​​
onDraw:视图的绘制工作;

  • 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、付费专栏及课程。

余额充值