如何在全屏显示片段

时间:2017-02-02 11:11:10

标签: android fragment fullscreen

如果我使用父布局作为容器..片段不可见 如果我使用子布局作为容器..它在wrap_content中显示片段,而不是全屏..甚至我的片段布局高度是match_parent

enter image description here

如何在全屏幕中打开片段

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

片段布局

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

片段加载代码

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

3 个答案:

答案 0 :(得分:1)

您可以使用按钮启动片段,在该按钮的点击监听器中将您的活动小部件的可见性设置为GONE,如下所示。

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

现在在回调接口中创建一个抽象方法

public interface MakeVisible {

void makeVisible();
}

现在在您的活动中

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


}

最后在您的片段中执行此操作

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

答案 1 :(得分:0)

跟着它 -

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

其主要布局。 ID flMain FrameLayout 是片段的容器。以下代码用于片段 -

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

}

以下代码适用于 FragmentTransaction

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

如果您遇到任何问题,请在此处发表评论。

答案 2 :(得分:0)

让您的活动成为FrameLayout

第一个孩子将保持与您想要隐藏的布局相同,

第二个孩子将是这个:

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

它的内在子(带有background =“@ drawable / container”)应该在片段内设置,

试试吧。