如何获取我在 XML 中设计的自定义视图并将其用作我的 MainActivity 中的新视图

问题描述 投票:0回答:1

我设计了我想要的自定义视图的样子:

<RelativeLayout>

    <ConstraintLayout>
        <ConstraintLayout>

            <TextView/>
            <ImageButton/>

        </ConstraintLayout>

        <ImageView/>

    </ConstraintLayout>
</RelativeLayout>

如何将其实现到我的 CustomView.java 文件中,以便我可以在 MainActivity.java 和 Activity_layout.xml 中使用它?

我知道我应该使用 LayoutInflater,但所有关于如何以及为什么使用 Kotlin 或印地语的解释我也不明白

java android xml android-custom-view layout-inflater
1个回答
0
投票

当然,我将指导您完成用 Java 实现自定义视图的过程。您是正确的,您可以使用 LayoutInflater 从布局 XML 文件中扩充您的自定义视图。让我们分解一下步骤:

创建您的自定义视图类: 首先创建一个扩展RelativeLayout 并用作自定义视图的Java 类。我们将此类命名为 MyCustomView。

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MyCustomView extends RelativeLayout {

    private ConstraintLayout innerConstraintLayout;
    private ConstraintLayout nestedConstraintLayout;
    private TextView textView;
    private ImageButton imageButton;
    private ImageView imageView;

    public MyCustomView(Context context) {
        super(context);
        init(context);
    }

    public MyCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.custom_view_layout, this, true);

        // Initialize your views
        innerConstraintLayout = findViewById(R.id.innerConstraintLayout);
        nestedConstraintLayout = findViewById(R.id.nestedConstraintLayout);
        textView = findViewById(R.id.textView);
        imageButton = findViewById(R.id.imageButton);
        imageView = findViewById(R.id.imageView);
    }

    // Add methods to interact with your custom views
}

创建您的自定义视图布局: 创建一个表示自定义视图的视觉布局的 XML 布局文件。我们将此布局文件命名为custom_view_layout.xml。将此文件放入您的 res/layout 目录中。

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

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/innerConstraintLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/nestedConstraintLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello" />

            <ImageButton
                android:id="@+id/imageButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher_foreground" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher_foreground" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</RelativeLayout>

在 MainActivity 中使用自定义视图: 现在,您可以在 MainActivity 中使用自定义视图。将自定义视图的实例添加到活动的布局 XML 文件 (activity_layout.xml) 中,或直接以编程方式将其添加到 MainActivity 类中。

MyCustomView customView = new MyCustomView(this);
// Set layout params if needed
setContentView(customView);
© www.soinside.com 2019 - 2024. All rights reserved.