简体   繁体   中英

I want my Android Fragment to cover the MainActivity screen (I don't want to see any MainActivity text, buttons, etc.)

This seems to be such a fundamental question that I'm embarrassed to ask it, but I'm so frustrated by my Fragment learning curve that I'll expose my ignorance.

An example in a textbook that cuts a lot of corners that make expanding them difficult, if they even work, had no button to click; MainActivity simply loaded FragmentA . OK, keep it basic. I get that.

So I added a button to MainActivity to click to load FragmentA , but the button shows on the FragmentA screen, sort of like this (not an actual screen shot, but close):

在此处输入图片说明

How do I prevent that? Should I use a second Activity instead of a Fragment ? Since this endeavor is to be utilized in a much larger project, I don't want to do anything considered not best practice. I realize that the main use of Fragment is to enable side-by-side "screens" on devices that are large enough. That's not what I want to do, but it IS possible to accomplish what I want with a Fragment , isn't it?

MainActivity.java

public class MainActivity extends Activity {

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

    setContentView(R.layout.activity_main);

   }

  public void btnLoadFragmentAByClick(View view)
  {
    FragmentA fragmentA;
                                                          fragmentA = new FragmentA();

    FragmentTransaction ft ;
                        ft = getFragmentManager().beginTransaction();
                        ft.replace(R.id.layout_container, fragmentA);
                        ft.addToBackStack("example");
                        ft.commit();
  }
}

FragmentA.java

public class FragmentA extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater _inflater,
                           ViewGroup                          _container,
                           Bundle         _savedInstanceState)
  {

        return _inflater.inflate(R.layout.fragment_a,
                                                              _container,
                             false);

    }
} 

activity_main.xml

<RelativeLayout
    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"
    tools:context           =".MainActivity" >

   <LinearLayout
       android:id           ="@+id/layout_container"
       android:orientation  ="vertical"
       android:layout_width ="wrap_content"
       android:layout_height="wrap_content"
       >
   </LinearLayout>

    <Button
        android:id           ="@+id/btnLoadFragmentA"
        android:text         ="Load Fragment A"
        android:onClick="btnLoadFragmentAByClick"
        android:layout_width ="wrap_content"
        android:layout_height="wrap_content"
        />

</RelativeLayout>

fragment_a.xml

<RelativeLayout
    xmlns:android           ="http://schemas.android.com/apk/res/android"
    android:layout_width    ="match_parent"
    android:layout_height   ="match_parent" >

    <TextView
        android:layout_width    ="wrap_content"
        android:layout_height   ="wrap_content"
        android:text            ="Layout for fragment A"
        android:textAppearance  ="?android:attr/textAppearanceLarge"
        >
    </TextView>

</RelativeLayout>

EDIT

I realize that I could hide the MainActiviy button (and any other objects) before loading FragmentA and show them after returning, but I was hoping for a one-or-two-line "fix".

How do I prevent that?

Well, to some extent, you don't, insofar as this has nothing to do with fragments.

Your activity_main.xml has the Button floating over top of the LinearLayout (???) that you are using for your fragment container. If you do not want the Button floating over top of the fragment container, then fix the layout file to not have the Button floating over top of the fragment container.

I realize that I could hide the MainActiviy button (and any other objects) before loading FragmentA and show them after returning, but I was hoping for a one-or-two-line "fix".

The typical solution for full-UI replacement using fragments is to have everything in fragments. Your replace() would replace your original fragment with a replacement. So, in this case, your Button would be managed by one fragment, and clicking the Button would replace() that fragment with another fragment. Given that your FragmentTransaction has addToBackStack() , pressing BACK would get rid of the replacement fragment and return you to your Button fragment.

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