【问题标题】:Fragments / Navigation Drawer Overlapping TableLayout?片段/导航抽屉重叠TableLayout?
【发布时间】:2017-11-02 17:06:32
【问题描述】:

我的代码需要帮助。我正在尝试使用 Android Studio 默认为您提供的导航抽屉代码。起始屏幕是 content_main.xml,问题是如果我在 content_main 上放置一个 textview 或其他任何东西,它会在切换时出现在其他片段/活动上。

我的代码(我有两个片段,一个 relativeLayout 和一个 TableLayout,但为了简单起见,我将使用一个):

Registration_fragment.java

public class Registration_fragment extends Fragment {
View myView;
@Nullable @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.registration_frag,container,false);
    return myView;
}

content_main.xml

<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.company.myName.projectname.MainActivity"
tools:showIn="@layout/app_bar_main">

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

registration_frag.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/testFrag">

<TableRow>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/first_name"
    android:layout_marginLeft="10dp"
    android:id="@+id/fnametext"
    />
  </TableRow>
  ... </TableLayout>

MainActivity.java

 public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    android.app.FragmentManager fragmentManager = getFragmentManager();
    if (id == R.id.nav_registration) {
        // Handle the camera action
        fragmentManager.beginTransaction().replace(R.id.content_frame,
                new Registration_fragment()).commit();

    } else if (id == R.id.nav_records) {
        fragmentManager.beginTransaction().replace(R.id.content_frame,
                new Records_fragment()).commit();
    } else if (id == R.id.nav_signup) {

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

我真的需要一些帮助来理解这个概念。

【问题讨论】:

    标签: android android-layout android-fragments navigation-drawer


    【解决方案1】:

    尝试将 R.id.content_frame 更改为 R.id.container。我看到你正在使用容器来充气。我有类似的设置,但它与 R.id.container 一起工作正常

    fragmentManager.beginTransaction().replace(R.id.container,
                    new Registration_fragment()).commit();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      • 2016-01-03
      • 1970-01-01
      相关资源
      最近更新 更多