动态地在Fragment Activity中包含另一个布局

时间:2014-03-23 14:29:52

标签: android android-layout android-fragments layout-inflater xml-layout

这是我的Home.xml布局文件

  <?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文件

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表示该情况下的索引或第二个孩子。

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

我怎样才能实现它?帮助!

2 个答案:

答案 0 :(得分:4)

我会在您想要布局的位置放置一些容器视图:

<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>

然后你可以像这样检索片段中的容器:

@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,如下所示:

flContainer.addView(subLayout);

如果您以后想要更改容器中的布局,可以这样做:

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

答案 1 :(得分:0)

使用LayoutParams。

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.BELOW, R.id.btn2);

r1.addView(layoutInflater.inflate(R.layout.test1, con , false),params);

或者将视图放在您的布局中,并使用View.setVisibility(View.GONE/View.VISIBLE)

切换可见性
相关问题