如何膨胀 child 布局

How to inflate a child layout

我开始学习 Android 开发,所以这个问题听起来有点菜鸟。 我尝试使用我创建的模板在 parent 布局中扩充 child 布局,但它抛出错误:指定的 child 已经有一个 parent.您必须先在 child 的 parent 上调用 removeView() 这是我的代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context="com.codingrabbit.spandan.rabbit101.MainActivity">

<include
    layout="@layout/toolbar"
    android:id="@+id/toolbar">
</include>

<LinearLayout
    android:id="@+id/contents"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar"
    android:layout_centerHorizontal="true">
</LinearLayout>

</RelativeLayout>

special_buttons.xml

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

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Subject"
    android:textSize="20sp"
    android:textColor="@color/white"
    android:textAlignment="center"
    android:background="@color/blue"
    android:id="@+id/subjectText"
    android:layout_gravity="center_horizontal" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="70dp"
    android:text="Details"
    android:textSize="18sp"
    android:textAlignment="center"
    android:textColor="@color/white"
    android:background="@color/green"
    android:paddingRight="16dp"
    android:paddingLeft="16dp"
    android:id="@+id/detailsText"
    android:layout_gravity="center_horizontal" />
</LinearLayout>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize">

</android.support.v7.widget.Toolbar>

MainActivity.java

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Toolbar Setup:
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("Main Menu");
    toolbar.setTitleTextColor(getResources().getColor(R.color.white));

    //Adding special buttons:
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout parent = (LinearLayout) findViewById(R.id.contents);
    for(int i=0; i<3; i++){
        View custom = inflater.inflate(R.layout.special_buttons, parent, true);
        TextView subText = (TextView) custom.findViewById(R.id.subjectText);
        TextView detailsText = (TextView) custom.findViewById(R.id.detailsText);
        subText.setText("Subject: "+(i+1));
        detailsText.setText("Details of subject " + (i + 1));
        parent.addView(custom);    //this is where the error is diplayed
    }
    setContentView(parent);
}
}

如果您提供 true 作为 inflate 的第三个参数,膨胀视图会自动添加到 parent(第二个参数)并返回父视图本身。因此,在您的情况下,您正在尝试向 parent 添加两次相同的视图,正如错误指出的那样,这是不可能的。

改变

inflater.inflate(R.layout.special_buttons, parent, true);

inflater.inflate(R.layout.special_buttons, parent, false);

你会得到你期望的行为

inflater.inflate(R.layout.special_buttons, parent, 假);