以编程方式在<include>中添加布局

时间:2017-12-19 09:15:53

标签: android android-layout include

我有以下xml:

fragment1.xml     

<include
    android:id="@+id/customized_layout"
    android:layout_width="match_parent"
    android:layout_height="40dp"
   android:layout_gravity="bottom"
 />
..
/>

在此片段中,如何以编程方式添加我想要包含在customized_layout中的布局?

我有以下内容:

@InjectView (R.id. customized_layout) RelativeLayout customLayout;
@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ButterKnife.inject(this, view);

      //customLayou.addLayout(...) ??
    }

2 个答案:

答案 0 :(得分:2)

使用LayoutInflater来充气xml。并将view添加到容器中。

LinearLayout container=(LinearLayout)findViewById(R.id.container);
View view= LayoutInflater.from(getActivity()).inflate(R.layout.item_view,null);
container.addView(view);

答案 1 :(得分:0)

假设您的动态布局为:

LinearLayout dynamicLayout = findViewById(R.id.dynamic_layout)
customLayout.addView(dynamicLayout)

在您的活动中:

{{1}}
相关问题