java - 在我的 fragment 中还显示了主要 Activity 的布局

标签 java android xml android-layout android-fragments

我是第一次使用 Fragments,很抱歉如果我做得非常愚蠢:D

我有一个抽屉导航 Activity 。当我单击第一个项目时,我想打开一个 fragment 。当我打开它时,不仅显示了Fragment的布局,而且还显示了我的MainActivity的布局,看起来很奇怪。

  [1]: /image/iqTAq.png MainActivity

  [2]: /image/kULQY.png Navigation Drawer

  [3]: /image/T2vDJ.png First Fragment

如何解决这个问题?

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

MainActivity(抽屉导航模板,我只在onNavigationItemSelected中编写,以便当我单击第一个项目时它会转到 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>

第一个 fragment Activity

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

}

至少还有 fragment 布局文件

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

我希望有人能帮助我:)

最佳答案

向您的 fragment 布局添加背景,它应该可以解决您的问题。

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

        ...

关于java - 在我的 fragment 中还显示了主要 Activity 的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45207833/

相关文章:

java - 运行时需要获取weblogic服务器用户名和密码

android - 即使应用被杀死,声音仍会继续播放

python从xml中提取数据并保存到excel

java - 使谷歌地图不是全屏Android应用程序

java - Sprites数组合并渲染 Sprite 时的

java - 如何并排显示 2 个相机预览?[对于纸板应用]

来自 URL 的 Android Picasso fragment 图片

c# - XML:如何将一个文件读入另一个文件

java - 如何获取线程的开始时间和结束时间?

java - android应用程序中如何处理域认证?