Dynamically include another layout in Fragment Activity

柔情痞子 提交于 2019-12-11 01:39:37

问题


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>

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

    }
}

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

How can I achieve it? Help!!


回答1:


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>

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

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



回答2:


Use 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);

Or put the view in your layout and toggle visibility with View.setVisibility(View.GONE/View.VISIBLE)



来源:https://stackoverflow.com/questions/22592192/dynamically-include-another-layout-in-fragment-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!