动态地将布局包含到片段内的另一个布局文件中

时间:2017-12-04 06:46:16

标签: android android-layout android-fragments include fragment

我有一个片段布局,我想动态地添加另一个子布局,

我试过这个https://stackoverflow.com/a/22592269/8476022

子布局xml

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

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:hint="Question"
    android:textColor="@color/colorTextBlack"
    android:textSize="16dp" />
</android.support.constraint.ConstraintLayout>

这是我的主要布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f2f2"
android:orientation="vertical">
<FrameLayout
    android:id="@+id/flContainer"
    android:layout_width="match_parent"
    android:layout_margin="20dp"
    android:layout_height="wrap_content"/>
</LinearLayout>

在我的片段中我包含了子布局

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_start_question, container, false);
     flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
     flContainer.addView(R.layout.sub_layout);
    return v;
}

但我在addView附近收到错误截图如下 enter image description here

任何人都可以帮助找到错误

6 个答案:

答案 0 :(得分:6)

您收到错误是因为 addView(View child)需要参数view并且您要添加FrameLayout ViewGroup

试试这种方式

LayoutInflater inflater = (LayoutInflater)   getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View myView = inflater.inflate(R.layout.sub_layout, null);;
flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
flContainer.addView(myView);

答案 1 :(得分:2)

添加视图获取View对象。您正在传递布局ID。

  

addView(查看视图)

您没有传递布局ID

  

flContainer.addView(R.layout.sub_layout); //错误

首先使用inflater对视图进行充气,然后将其添加到容器中。

 View view=LayoutInflater.from(getContext()).inflate(R.layout.yourView,null);
  yourContainer.addView(view);

答案 2 :(得分:1)

您需要扩充布局以添加视图,请参阅this

View child = getLayoutInflater().inflate(R.layout.sub_layout, null);
flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
flContainer.addView(child);

答案 3 :(得分:1)

您想使用:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
    // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_start_question, container, false);
    // flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
        // flContainer.addView(R.layout.sub_layout);
    LinearLayout linPhoneNumber = (LinearLayout) v.findViewById(R.id.linPhoneNumber);
    View viewPhoneNumber  =  getActivity().getLayoutInflater().inflate(R.layout.fragment_contact_phone, null);
    LinearLayout linPhoneLayout = (LinearLayout) viewPhoneNumber.findViewById(R.id.linPhoneLayout);
    TextView txtPhoneNumber = (TextView) viewPhoneNumber.findViewById(R.id.txtPhoneNumber);
    txtPhoneNumber.setText(phone);
    linPhoneNumber.addView(viewPhoneNumber);
    return v;
}

在fragment_start_question.xml中放置一个线性布局:

<LinearLayout
    android:id="@+id/linPhoneNumber"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical"></LinearLayout>

然后是fragment_contact_phone.xml

<LinearLayout
    android:id="@+id/linPhoneLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/margin_top_10"
    android:orientation="horizontal"
    android:padding="@dimen/margin_top">

    <ImageView
        android:id="@+id/imgCall"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="center_vertical"
        android:src="@drawable/icon_call" />

    <Textview
        android:id="@+id/txtPhoneNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="@dimen/margin_top_1"
        android:textColor="@color/menu_text_color"
        android:textSize="@dimen/margin_top_1"/>
</LinearLayout>

答案 4 :(得分:0)

您只传递布局ID,首先定义布局,如:

LinearLayout sublayout = (LinearLayout)v.findViewBYId(R.layout.sub_layout)
flContainer.addView(sublayout);

答案 5 :(得分:0)

使用以下内容替换onCreateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_start_question, container, false);
    flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
    View subView = inflater.inflate(R.layout.sub_layout, null);
    flContainer.addView(subView);
    return v;
}