我尝试使用
实现材质导航抽屉android.support.v4.widget.DrawerLayout
android.support.v7.widget.Toolbar
android.support.design.widget.NavigationView
问题是NavigationView&工具栏始终由FrameContent覆盖。 FrameContent只是一个片段。
*注意:我曾尝试将LinearLayout替换为RelativeLayout作为容器,但不起作用。
activity_layout.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@android:id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_alignParentTop="true"/>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"/></android.support.v4.widget.DrawerLayout>
fragment.xml之
<FrameLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"/></FrameLayout>
ActivityMain.java
public class MainActivity extends AppCompatActivity{
protected Toolbar mToolbar;
protected NavigationView mNavigationView;
protected DrawerLayout mDrawerLayout;
protected FrameLayout container;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.material_activity_layout);
this.mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(this.mToolbar);
this.mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
this.mNavigationView.inflateHeaderView(R.layout.nav_header);
this.mNavigationView.inflateMenu(R.menu.drawer);
this.mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
ActionBarDrawerToggle defaultActionBarDrawerToggle = new ActionBarDrawerToggle(
this,
this.mDrawerLayout,
this.mToolbar,
R.string.app_name,
R.string.app_name) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
this.mDrawerLayout.setDrawerListener(defaultActionBarDrawerToggle);
defaultActionBarDrawerToggle.syncState();
this.mToolbar.setTitle("Test Toolbar");
this.mToolbar.setBackgroundColor(Color.BLUE);
getFragmentManager().beginTransaction().add(android.R.id.content, new InFragment(), "fragment").commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
public static class InFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment, container, false);
}
}
}
结果
答案 0 :(得分:-1)
您可以LinearLayout
使用android:orientation="vertical"
代替RelativeLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_alignParentTop="true"/>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>