在Fragment Activity中动态包含另一个布局 [英] Dynamically include another layout in Fragment Activity

查看:163
本文介绍了在Fragment Activity中动态包含另一个布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Home.xml布局文件

This is my Home.xml layout file

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

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="click" 
        />

    **<I want to include another layout here on a click of a button dynamically on click of a button >**

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
       android:layout_below="@id/btn2"
        android:text="hello this is text..see above" />

</RelativeLayout>

这是我的Home.java文件

This is my Home.java file

public class Home extends Fragment implements OnClickListener {
    Button b1;
    RelativeLayout r1;
    View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.home, container, false);
        b1 = (Button) rootView.findViewById(R.id.btn2);
        b1.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View arg0) {
        ViewGroup con = null;
        r1 = (RelativeLayout) rootView.findViewById(R.id.rellay);
        LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        r1.addView(layoutInflater.inflate(R.layout.test1, con , false));

    }
}

当我单击按钮时,它将test1.xml布局文件放置在home.xml布局的顶部.我需要它在按钮和文本之间.我之前在堆栈上检查了此代码,但似乎无法与FragmentActivity一起使用.这里1表示索引或在这种情况下的第二个孩子.

When I click on the button it places the test1.xml layout file at the top of the home.xml layout. I need it to be between the button and the text. I checked this code on stack before but it doesn't seem to work with the FragmentActivity. Here 1 indicates the index or the second child in this case.

rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) );

如何实现?帮忙!

推荐答案

我将一些容器视图放在您希望布局像这样的位置:

I would put some container view at the position you want your layout to be like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/rellay"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:background="@drawable/placesgradient">

    <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="click"
            />

    <FrameLayout 
            android:id="@+id/flContainer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_below="@id/btn2"
            android:text="hello this is text..see above" />

</RelativeLayout>

然后您可以像这样在Fragment中检索容器:

Then you can retrieve the container in your Fragment like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.home, container, false);

    // Retrieve your container
    flContainer = rootView.findViewById(R.id.flContainer);

    b1 = (Button) rootView.findViewById(R.id.btn2);
    b1.setOnClickListener(this);

    return rootView;
}

稍后,当您要添加布局时,可以向FrameLayout添加子级,如下所示:

And later when you want to add your layout you can add a child to the FrameLayout like this:

flContainer.addView(subLayout);

如果以后要更改容器中的布局,可以按以下方式进行操作:

If you later want to change the layout in your container you can do it like this:

flContainer.removeAllViews();
flContainer.addView(otherLayout);

这篇关于在Fragment Activity中动态包含另一个布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆