【发布时间】:2018-02-14 08:01:21
【问题描述】:
我正在使用具有抽屉布局的导航视图和侧导航视图中的自定义布局作为菜单,并将其包含在导航视图中,但注意是可见的,我还尝试添加其他类型的视图,但导航视图中没有显示任何内容。 下面是我使用抽屉布局和导航视图以及主要活动类的主要活动 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/Drawer"
tools:context="com.example.minhasoft_pc.drawer.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4"
android:orientation="vertical">
<ImageView
android:layout_margin="10dp"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher_round"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_margin="5dp"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher_round"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_margin="10dp"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher_round"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:layout_marginEnd="-65dp"
android:layout_marginRight="-65dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/drawer_menu"/>
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
活动类
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerlayout ;
private ActionBarDrawerToggle mToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ActionBar bar = getActionBar();
// bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
mDrawerlayout = (DrawerLayout) findViewById(R.id.Drawer) ;
mToggle = new ActionBarDrawerToggle(this,mDrawerlayout,R.string.draweropen,R.string.drawerclosed) ;
mDrawerlayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item))
{
return true ;
}
return super.onOptionsItemSelected(item);
}
}
【问题讨论】:
-
NavigationView并不意味着任意添加Views。如果您没有使用NavigationView来实现其特定功能,则将其删除,并将LinearLayout设置为抽屉,方法是在其上设置android:layout_gravity="start",并为其指定一个明确的layout_width,例如240dp. -
删除
NavigationView中的LinearLayout。只是<include>里面的菜单布局。 -
@MikeM。我同意,这就是为什么我赞成你的评论。
-
@Mike M. 谢谢它的工作!但现在出现了一个不同的问题,我添加的布局没有显示它应该显示的背景颜色..
-
对不起,我的坏人!只是一个愚蠢的错误!
标签: android android-layout drawerlayout navigationview android-navigationview