【问题标题】:Change content_main XML in Basic Navigation Drawer Template更改基本导航抽屉模板中的 content_main XML
【发布时间】:2018-12-13 12:44:46
【问题描述】:

我真的是 android studio 的新手,并且遇到了一个非常基本的问题。 我看过一些关于这个问题的帖子,但没有一个能真正解决我的问题。

问题: 如何更改 NavigationDrawer 模板中的片段?模板提供了一个环境,其中所谓的 content_main.xml 应该被更改为另一个片段。我不知道如何完成这个

我尝试了什么:由于我无法更改 content_main.xml,我更改了整个 xml,其中包括 content_main.xml。简单看一下我的代码就更容易理解了。

模板 app_bar_main.xml 设置片段的外观并包含 content_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    android:id="@+id/content_main_frame"
    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"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</FrameLayout>

在导航抽屉 main_activity 中,我像这样更改我的片段:

public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        Fragment newFragment;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if (id == ..) {
        } else if (id == R.id.nav_emergencies) {
            transaction.replace(R.id.content_main_frame, new Drawer_Emergencies());
        transaction.addToBackStack(null);
        transaction.commit();

}}

您可能会看到该事务替换了整个 app_bar_main,但我认为我应该只更改 app_bar_main.xml 中包含的 content_main.xml。我该如何完成?

当我实施裘德的建议时,我得到了更新的结果。片段在正确的位置,但不知何故,content_main_frame.xml(hello world)仍然显示。

【问题讨论】:

    标签: android android-studio


    【解决方案1】:

    也许您应该尝试在您的content_main xml 文件中创建一个framelayout。 然后将新片段加载到框架布局中。这就像为新片段创建一个框架。 然后像这样加载它:

    transaction.add(R.id.yourframelayoutid, new Drawer_Emergencies()).commit();
    

    【讨论】:

    • 谢谢!那是一个很好的镜头。可悲的是,它并没有取代 content_main 框架布局,而是将其置于顶部。这意味着 content_main 仍然存在而不是被替换..
    • 我顺便编辑了我的帖子并附上了截图,因此你可以明白我的意思。 :)
    • @Christian.gruener 也许您应该包含完整的 XML 布局(包括 app_bar_main.xml) 我需要了解片段应该替换的内容。然后尝试调整框架布局的大小,使其不包含整个屏幕并查看
    • 嘿,我最初是这样做的。但这引发了片段 Drawer_emergency 位于工具栏下的最初问题.. :)
    【解决方案2】:

    通过与 Mike.M 的讨论,我能够解决我的问题,而他提供了方法。为了使这个线程可以关闭,我想在这里发布解决方案,引用 Mike.M :

    如果你想用 FragmentTransaction 替换一些 View,这些 View 应该在它们自己的 Fragment 中,你可以在启动时加载,在 Activity 的 onCreate() 中。然后,您的后续 replace() 事务将按预期工作,前提是您为所有这些事务传递相同的 R.id;即,前提是您将它​​们从同一个 ViewGroup 中交换出来。 (顺便说一句,当您想 ping 我时,请在我的用户名前加上 @。当 cmets 中有多个其他用户时,除非您直接将其发送给某个用户,否则我们不会收到通知。)

    产生以下分步解决方案:

    1. 我必须制作空 Framelayout,例如使我的 content_main.xml 为空。
    2. 为初始内容制作一个单独的片段,并在开始时将其加载到 content_main.xml 中..
    3. 打开一个新抽屉后,我是片段容器,这是我的 Framelayout 和一个新片段..

    【讨论】:

      【解决方案3】:

      你可以这样做:

      content_main.xml:

      <?xml version="1.0" encoding="utf-8"?>
      <FrameLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"
          android:id="@+id/content_frame">
      
      </FrameLayout>
      

      然后

              transaction.replace(R.id.content_frame, new Drawer_Emergencies());
      

      【讨论】:

      • 嘿 F43nd1r。这也是裘德的建议。这是一个很好的镜头,我实现了。我仍然存在的唯一问题是 concent_frame 没有被我的新片段替换。该片段位于 content_frame 的顶部。我该如何解决这个问题?
      • 我编辑了我的帖子顺便说一句,包括截图,因此你可以明白我的意思。
      • stackoverflow.com/questions/14810348/… 建议框架布局必须为空。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      相关资源
      最近更新 更多