我有一个正确显示我的ActionBar及其项目的活动,但是当我尝试向该活动添加片段时,ActionBar会在启动该片段时消失,或者如果我尝试包含ActionBar,它会显示栏,但没有标题或MenuOptions。
希望我尝试实现的是一个活动,除非另有说明,否则所有碎片都使用相同的ActionBar。
MainActivity.Java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final HomeFragment homeFragment = new HomeFragment();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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);
if (savedInstanceState == null) {
getFragmentManager()
.beginTransaction()
.add(R.id.main_container, homeFragment)
.addToBackStack(null)
.commit();
}
}
@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_activity_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;
} else if (id == R.id.search_mag_icon){
// Open intent here
Intent i = new Intent(getApplicationContext(), SearchActivity.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
HomeFragment.java
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Book icon and text -->
<ImageView
android:id="@+id/bookIcon"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="50dip"
android:layout_marginTop="110dip"
android:src="@drawable/book" />
<TextView
android:id="@+id/bookLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bookIcon"
android:layout_marginLeft="20dip"
android:text="@string/bookLabel"
android:textColor="@color/colorAlt"
android:textStyle="bold" />
<!-- Barcode icon and text -->
<ImageView
android:id="@+id/barcodeIcon"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginLeft="260dip"
android:layout_marginTop="120dip"
android:src="@drawable/barcode" />
<TextView
android:id="@+id/barcodeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/barcodeIcon"
android:layout_marginLeft="260dip"
android:paddingTop="5dip"
android:text="@string/barcodeLabel"
android:textColor="@color/colorAlt"
android:textStyle="bold" />
<!-- Message icon and text -->
<ImageView
android:id="@+id/messageIcon"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="50dip"
android:layout_marginTop="350dip"
android:src="@drawable/email" />
<TextView
android:id="@+id/messageLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/messageIcon"
android:layout_marginLeft="55dip"
android:text="@string/messageLabel"
android:textColor="@color/colorAlt"
android:textStyle="bold" />
<!-- List icon and text -->
<ImageView
android:id="@+id/listIcon"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginLeft="260dip"
android:layout_marginTop="350dip"
android:src="@drawable/list" />
<TextView
android:id="@+id/listLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/listIcon"
android:layout_marginLeft="273dip"
android:paddingTop="5dip"
android:text="@string/listLabel"
android:textColor="@color/colorAlt"
android:textStyle="bold" />
</RelativeLayout>
</FrameLayout>
activity_main
<?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:id="@+id/drawer_layout"
android:background="#FFF"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/app_bar_main" />
<FrameLayout
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="@color/colorPrimaryDark"
app:itemTextColor="#FFF"
app:itemIconTint="#FFF"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<include layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Book icon and text -->
<ImageView
android:id="@+id/bookIcon"
android:src="@drawable/book"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="50dip"
android:layout_marginTop = "110dip"
/>
<TextView
android:id="@+id/bookLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bookLabel"
android:textColor="@color/colorAlt"
android:textStyle="bold"
android:layout_marginLeft="20dip"
android:layout_below="@+id/bookIcon" />
<!-- Barcode icon and text -->
<ImageView
android:id="@+id/barcodeIcon"
android:src="@drawable/barcode"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginLeft="260dip"
android:layout_marginTop = "120dip"
/>
<TextView
android:id="@+id/barcodeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/barcodeLabel"
android:textColor="@color/colorAlt"
android:textStyle="bold"
android:layout_below="@+id/barcodeIcon"
android:paddingTop="5dip"
android:layout_marginLeft="260dip"/>
<!-- Message icon and text -->
<ImageView
android:id="@+id/messageIcon"
android:src="@drawable/email"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="50dip"
android:layout_marginTop = "350dip"
/>
<TextView
android:id="@+id/messageLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/messageLabel"
android:textColor="@color/colorAlt"
android:textStyle="bold"
android:layout_marginLeft="55dip"
android:layout_below="@+id/messageIcon" />
<!-- List icon and text -->
<ImageView
android:id="@+id/listIcon"
android:src="@drawable/list"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginLeft="260dip"
android:layout_marginTop = "350dip"
/>
<TextView
android:id="@+id/listLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/listLabel"
android:textColor="@color/colorAlt"
android:textStyle="bold"
android:layout_below="@+id/listIcon"
android:paddingTop="5dip"
android:layout_marginLeft="273dip"/>
</RelativeLayout>
感谢您提前提供任何帮助。
答案 0 :(得分:0)
修改布局
将ToolBar
置于LinearLayout
内,将Fragment
改为FrameLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/app_bar_main" />
<FrameLayout
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
从Fragment
Activity
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.main_container,homeFragment));
fragmentTransaction.commit();