Android - 自定义控件

1. 添加自定义Layout

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
custom_item.xml 代码:

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

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:scaleType="centerInside" />

    <TextView
        android:id="@+id/item_text"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical" />

</LinearLayout>

2. 添加自定义控件的类

CustomItem.java 代码:

public class CustomItem extends FrameLayout {

    private Context mContext;
    private View mView;
    private ImageView mItemImage;
    private TextView mItemText;

    public CustomItem(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public CustomItem(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CustomItem(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public CustomItem(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs){
        mContext = context;

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.custom_item, this, true);

        mItemImage = findViewById(R.id.item_image);
        mItemText = findViewById(R.id.item_text);

    }

}

3. 自定义控件的属性

3.1 添加 attrs_custom_item.xml

在这里插入图片描述

<resources>
    <declare-styleable name="CustomItem">
        <attr name="custom_item_text" format="string" />
        <attr name="custom_item_image" format="color|reference" />
    </declare-styleable>
</resources>

3.2 CustomItem.java类中添加属性的处理

在这里插入图片描述
CustomItem.java类最终代码为:

public class CustomItem extends FrameLayout {

    private Context mContext;
    private View mView;
    private ImageView mItemImage;
    private TextView mItemText;

    public CustomItem(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public CustomItem(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CustomItem(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public CustomItem(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs){
        mContext = context;

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.custom_item, this, true);

        mItemImage = findViewById(R.id.item_image);
        mItemText = findViewById(R.id.item_text);

        TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.CustomItem);
        mItemText.setText(a.getString(R.styleable.CustomItem_custom_item_text));
        mItemImage.setImageResource(a.getResourceId(R.styleable.CustomItem_custom_item_image, R.drawable.image3));
    }

}

4. 在activity_main.xml中添加自定义控件

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

    <com.example.customview.CustomItem
        android:layout_width="match_parent"
        android:layout_height="40dp"
        app:custom_item_text="第一行"
        app:custom_item_image="@drawable/image1"/>

    <com.example.customview.CustomItem
        android:layout_width="match_parent"
        android:layout_height="40dp"
        app:custom_item_text="第二行"
        app:custom_item_image="@drawable/image2"/>

</LinearLayout>

5. 效果

在这里插入图片描述

6. 代码

https://gitee.com/jie-xio/android_samples/tree/master/CustomControl




7. 更优步骤

以上添加自定义 custom_item.xmlCustomItem.javaattrs_custom_item.xml 的步骤,在Android studio中有简洁的步骤,自动创建这些文件:
在这里插入图片描述
在这里插入图片描述
自动会添加上这些文件,自己只需要按照本文上面的相应内容做修改即可
在这里插入图片描述

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中,您可以通过创建自定义视图类来实现自定义控件。下面是一些步骤,帮助您开始创建自己的自定义控件: 1. 创建一个新的Java类,该类将扩展现有的Android视图类(例如,TextView、Button等),或者直接扩展View类以创建一个全新的自定义视图。 2. 在类中实现构造函数和必要的方法。您可能需要重写onDraw()方法来处理绘制自定义视图的逻辑。还可以重写其他方法(例如,onMeasure()、onLayout()等),以便根据需要对自定义视图进行测量和布局。 3. 在XML布局文件中使用您的自定义控件。在布局文件中添加一个对应于您自定义控件类的标签,并根据需要设置属性。例如,如果您的自定义控件具有自定义属性,您可以在XML中设置这些属性。 4. 在Java代码中使用您的自定义控件。在Activity或Fragment中,通过findViewById()方法找到布局文件中的自定义控件,并使用它们。 5. 可选步骤:如果您的自定义控件需要处理用户交互事件(例如,点击事件),您可以重写相应的方法(例如,onTouchEvent())来实现所需的功能。 这只是一个简单的指南,让您了解如何在Android Studio中创建自定义控件。在实际开发过程中,您可能需要更多的步骤和代码来实现您的具体需求。您可以参考Android开发文档和其他教程,以获取更详细的指导和示例代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值