[英]Fragments / Navigation Drawer Overlapping TableLayout?
I need help with my code. 我的代码需要帮助。 I'm trying to use the Navigation Drawer code Android Studio gives you by default. 我正在尝试使用Android Studio默认为您提供的导航抽屉代码。 The starting screen is the content_main.xml and the problem is if I put a textview or anything else on the content_main, it appears on the other fragment/activities when switching. 起始屏幕是content_main.xml,问题是如果我在content_main上放置了文本视图或其他内容,则在切换时它会出现在其他片段/活动上。
My Code (I have two fragments, A relativeLayout & a TableLayout but I'll use one for simplicity): 我的代码(我有两个片段,一个relativeLayout和一个TableLayout,为简单起见,我将使用其中一个):
Registration_fragment.java 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 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 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 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;
}
I could really use some help understanding the concept. 我真的可以用一些帮助来理解这个概念。
Try changing R.id.content_frame to R.id.container. 尝试将R.id.content_frame更改为R.id.container。 I see that you are using container to inflate. 我看到您正在使用容器进行充气。 I have similar setup but it is working fine with R.id.container 我有类似的设置,但与R.id.container正常工作
fragmentManager.beginTransaction().replace(R.id.container,
new Registration_fragment()).commit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.