【问题标题】:In my Fragment is the Layout of Mainactivity also shown在我的片段中还显示了 Mainactivity 的布局
【发布时间】:2017-12-25 17:52:36
【问题描述】:

我是第一次使用 Fragments,如果我确实很愚蠢,很抱歉:D

我有一个导航抽屉活动。当我在第一个项目上单击它时,我想打开一个片段。打开的时候,不仅有Fragment的Layout,还有我的MainActivity的layout,看起来很奇怪。

  [1]: https://i.stack.imgur.com/iqTAq.png MainActivity

  [2]: https://i.stack.imgur.com/kULQY.png Navigation Drawer

  [3]: https://i.stack.imgur.com/T2vDJ.png First Fragment

我该如何解决这个问题?

如果你需要,这是我的代码:

MainActivity(Navigation Drawer Templated,我只写在onNavigationItemSelected中,这样点击第一个item的时候就到fragment了:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            setTitle("First Fragment");
            first first = new first();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.fragment, first).commit();
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

content_main(应该属于MainActivity)

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.gsr.fragmenttest.MainActivity"
    tools:showIn="@layout/app_bar_main">

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


      <TextView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:text="This is the Mainactivity, no fragemnt"/>

   </FrameLayout>

</RelativeLayout>

第一个片段活动

public class first extends Fragment {

    Button btn;
    TextView textView;

    public first() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_first, container, false);
        btn=(Button)v.findViewById(R.id.button);
        textView=(TextView)v.findViewById(R.id.textView);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    textView.setText("Nice, it works !!");
            }
        });
        // Inflate the layout for this fragment
        return v;
    }

}

至少还有片段布局文件

<FrameLayout 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="com.gsr.fragmenttest.first">

    <!-- TODO: Update blank fragment layout -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">



        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Hello Fragment"
            android:textSize="50dp"/>

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"/>


    </RelativeLayout>

</FrameLayout>

我希望有人可以帮助我:)

【问题讨论】:

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


    【解决方案1】:

    我认为问题出在您的活动布局中

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
    
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </FrameLayout>
    
    </android.support.v4.widget.DrawerLayout>
    

    尝试像上面那样更改您的主要活动布局。希望它能解决你的问题。

    【讨论】:

    • 我先在 content_main 和 fragment 中尝试过,但没有任何改变
    【解决方案2】:

    为您的片段布局添加一个背景,它应该可以解决您的问题。

    <FrameLayout 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:background="@color/white"                 //Your background color
        tools:context="com.gsr.fragmenttest.first">
    
        <!-- TODO: Update blank fragment layout -->
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            ...
    

    【讨论】:

    • 谢谢,我认为这不是方法,因为 content_main 仍在加载......但它有效:D
    • Content main 将以任何一种方式加载,因为片段显示在您的容器中,位于活动“上方”。
    【解决方案3】:

    1) 实现first.OnOnFragmentInteractionListener

    2) 在 onNavigationItemSelected 方法中放入这段代码

     if (id == R.id.nav_camera) {
            setTitle("First Fragment");
            first first = new first();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.fragment, new first()).commit();
    

    3)在第一个片段中使用这一行

    import android.app.Fragment;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多