【发布时间】:2018-03-21 13:21:03
【问题描述】:
我创建了一个底部导航,当点击图标时,片段不会加载到框架布局上,而是显示在底部导航上
这是MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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"
android:id="@+id/main_drawer"
tools:context="com.safarpar.safarpar.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.BottomNavigationView
android:id="@+id/main_nav"
android:layout_width="match_parent"
android:layout_height="@dimen/_56sdp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
app:menu="@menu/bottom_nav_menu">
</android.support.design.widget.BottomNavigationView>
<FrameLayout
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/main_nav">
</FrameLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/nav_view"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
还有MainActivity.java
mFrame=(FrameLayout)findViewById(R.id.main_frame);
mbottomNavigationView=(BottomNavigationView)findViewById(R.id.main_nav);
mDrawerLayout=(DrawerLayout)findViewById(R.id.main_drawer);
mToggle = new
ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
//navigationdrawer
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//OnclickHandeler();
homeFragment= new HomeFragment();
bookingFragment= new BookingFragment();
mbottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
setFragment(homeFragment);
return true;
case R.id.nav_booking:
setFragment(bookingFragment);
default:
return true;
}
}
private void setFragment(android.support.v4.app.Fragment Fragment) {
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_nav,Fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
还有Fragment.xml
<RelativeLayout 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.safarpar.safarpar.HomeFragment">
<LinearLayout
android:id="@+id/Linear1"
android:layout_width="match_parent"
android:layout_height="@dimen/_200sdp"
android:background="@drawable/front_slider"
android:gravity="top"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="@+id/linear_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/Linear1"
android:layout_marginTop="@dimen/_10sdp"
android:orientation="horizontal"
android:padding="@dimen/_10sdp">
<Button
android:id="@+id/main_flight"
android:layout_width="@dimen/_50sdp"
android:layout_height="@dimen/_50sdp"
android:layout_marginLeft="@dimen/_19sdp"
android:layout_marginRight="@dimen/_7sdp"
android:background="@drawable/flight_button_state" />
<Button
android:id="@+id/main_hotel"
android:layout_width="@dimen/_50sdp"
android:layout_height="@dimen/_50sdp"
android:layout_marginLeft="@dimen/_10sdp"
android:layout_marginRight="@dimen/_7sdp"
android:background="@drawable/hotel_button_state" />
<Button
android:id="@+id/main_bus"
android:layout_width="@dimen/_50sdp"
android:layout_height="@dimen/_50sdp"
android:layout_marginLeft="@dimen/_7sdp"
android:layout_marginRight="@dimen/_10sdp"
android:background="@drawable/bus_button_state" />
<Button
android:id="@+id/main_cab"
android:layout_width="@dimen/_50sdp"
android:layout_height="@dimen/_50sdp"
android:layout_marginLeft="@dimen/_7sdp"
android:layout_marginRight="@dimen/_10sdp"
android:background="@drawable/cab_button_state" />
</LinearLayout>
</RelativeLayout>
【问题讨论】:
-
好像你的主布局搞砸了。首先检查布局中的
Framelayout是否可见。 -
检查您在 Fragment 类中使用的片段版本,并检查 framlayout id 以及检查其是否可见
-
fragmentTransaction.replace(R.id.main_nav,Fragment);检查这个通过你的框架布局ID
-
“而不是显示在底部导航” - 这就是你告诉它去的地方:
fragmentTransaction.replace(R.id.main_nav,Fragment);。如果你想要它在FrameLayout中,请将main_nav更改为main_frame。
标签: android xml android-fragments android-framelayout