【问题标题】:Views in NavigationView are not showing/not visibleNavigationView 中的视图未显示/不可见
【发布时间】: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。只是&lt;include&gt;里面的菜单布局。
  • @MikeM。我同意,这就是为什么我赞成你的评论。
  • @Mike M. 谢谢它的工作!但现在出现了一个不同的问题,我添加的布局没有显示它应该显示的背景颜色..
  • 对不起,我的坏人!只是一个愚蠢的错误!

标签: android android-layout drawerlayout navigationview android-navigationview


【解决方案1】:

您必须包括您的导航。像这样的抽屉菜单项,请检查以下 xml 代码作为参考:

这里包含app:menu="@menu/nav_drawer_items"

您可以参考这个问题的解决方案:Align custom view to left of navigation drawer

 <?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.NavigationView
        android:id="@+id/nav1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_drawer_items"
        app:itemBackground="@color/app_bg_color"
        app:itemIconTint="@color/text_white"
        app:itemTextColor="@color/text_white"
        android:background="@color/bottom_navigation_color"
        android:fitsSystemWindows="true"
        android:theme="@style/NavigationView" />
</android.support.v4.widget.DrawerLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    相关资源
    最近更新 更多