如何给孩子布局充气

时间:2016-07-19 10:13:29

标签: java android xml layout-inflater

我开始学习Android开发,所以这个问题可能听起来有些愚蠢。 我尝试使用我创建的模板在父布局中扩展子布局,但是它会抛出错误:指定的子级已经有父级。您必须首先在孩子的父母上调用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);
}
}

2 个答案:

答案 0 :(得分:1)

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

更改

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

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

你会得到你期望的行为

答案 1 :(得分:0)

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