如何在 Android 的 RelativeLayout class 中 Inflate XML 布局?

How to Inflate XML layout in RelativeLayout class in Android?

在我的视图包中创建 RelativeLayout class 并在 class 中膨胀 XML 文件 用于主要 activity 但没有任何错误,模拟器已停止

import android.content.Context;
import android.content.res.AssetManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.sibdiet.R;

public class AboutView extends RelativeLayout {

    private TextView about_txt;
    private AssetManager assets;

    public AboutView(Context context) {
        super( context );
        initializeViews( context );
    }

    public AboutView(Context context, AttributeSet attrs) {
        super( context, attrs );
        initializeViews( context );
    }

    private void initializeViews(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.aboutus_layout, this);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        about_txt = (EditText) findViewById(R.id.aboutus_txt);
    }

    public AssetManager getAssets() {
        return assets;
    }
}

我需要充气aboutus_layout.xml

<merge
    xmlns:android="http://schemas.android.com/apk/res/android">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/aboutus_txt"
            android:layout_width="61dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="16dp"
            android:layout_marginTop="16dp"
            android:text="@string/about"
            android:textColor="@color/Green"
            android:textSize="20sp"/>

    </RelativeLayout>
</merge>

在主视图中使用自定义视图 activity main_activity.xml

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

<android.support.constraint.ConstraintLayout 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"
    android:background="#DCDCDC"
    tools:context="net.sibdiet.MainActivity">

    <net.sibdiet.View.AboutView
        android:id="@+id/about_us_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</android.support.constraint.ConstraintLayout>

blah您的 post 似乎包含格式不正确的代码。请使用代码工具栏按钮或 CTRL+K 键盘快捷键将所有代码缩进 4 个空格。如需更多编辑帮助,请单击 [?] 工具栏图标。

您的代码存在的问题是:

1) 将 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 替换为 LayoutInflater inflater = LayoutInflater.from(getContext())

2) 从您的 aboutus_layout.xml 中删除 RelativeLayout,因为那里多余。使您的视图 (AboutView) 位于视图层次结构的根部。