简体   繁体   中英

How to display fragment on full screen?

If I am using parent layout as container.. fragment is not visible and if i use child layout as container.. its displaying fragment in wrap_content, not in full screen.. even my fragment layout height is match_parent

在此处输入图片说明

How to open fragment in full screen?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/container">  <!-- this id identifies fragment container -->

    <LinearLayout
        style="@style/innerContainer"
        android:layout_margin="6dp"
        android:background="@drawable/container">

    </LinearLayout>

</LinearLayout>

fragment layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.thevisionspark.jobsite.AppliedUsers">

    <LinearLayout
        style="@style/innerContainer">

        <TextView
            style="@style/headings"
            android:text="Applied Users"
            android:layout_gravity="center_horizontal"/>


    </LinearLayout>


</LinearLayout>

fragment loading code

FragmentTransaction fm = getSupportFragmentManager().beginTransaction();
                    AppliedUsers frag = new AppliedUsers();
                    frag.setArguments(bundle);

                    if(getSupportFragmentManager().getFragments() == null)
                        fm.add(R.id.container, frag).commit();

                    else fm.replace(R.id.container, frag).commit();

You can use a button to launch the fragment, inside that button's click listener set visibility of you Activity's widgets to GONE like this.

yourButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            yourWidget.setVisibility(View.GONE);
            anotherWidget.setVisibility(View.GONE);


            Fragment yourFragment = new YourFragment();
            FragmentManager fragmentManager = getFragmentManager();

            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.your_activity_layout, YourFragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });

Now Create an abstract method inside a callback interface

public interface MakeVisible {

void makeVisible();
}

now inside your activity

public class Activity extends FragmentActivity implements MakeVisible{
@Override
public void makeVisible() {
   yourWidget.setVisibility(View.VISIBLE);
    anotherWidget.setVisibility(View.VISIBLE);


}

Finally do this in your fragment

@Override
public void onDetach() {
    MakeVisible makeVisible = (MakeVisible) getActivity();
    makeVisible.makeVisible();
    super.onDetach();
}

Follow it -

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

        <include
            layout="@layout/layout_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/flMain"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>

    </android.support.v4.widget.NestedScrollView>

</LinearLayout>

Its main layout. Here FrameLayout with id flMain is the container for fragment. And following code is for the fragment -

public class CustomFragment  extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        View customeFragment = inflater.inflate(R.layout.layout_fragment, container, false);


        return customeFragment;
    }
}

And following code is for FragmentTransaction .

CustomeFragment customeFragment = new CustomeFragment();
getFragmentManager().beginTransaction()  .replace(R.id.flMain, customeFragment).addToBackStack(null).commit();

If you face any problem then put a comment on here.

Make your activity a FrameLayout

The first child will stay the same with the layout you want to hide later ,

The second child will be this :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/container">  <!-- this id identifies fragment container -->
</LinearLayout>

The inner child of it(with the background="@drawable/container") should be set inside the fragment,

Try it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM