【问题标题】:Android NavigationDrawer transparencyAndroid NavigationDrawer 透明度
【发布时间】:2017-06-16 15:01:47
【问题描述】:

我已经按照 this tutorial 使用 android-support-v4 实现了 NavigationDrawer

我的结果在这张截图上。

问题是如何在 Drawer 上移除或配置这些透明度,因为这些透明度在背景文本中看起来很糟糕。

更新: 这是我的布局配置,背景设置为深色

<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

    <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#111111"/>
</android.support.v4.widget.DrawerLayout>

【问题讨论】:

  • 你有没有得到这个答案?我也有同样的问题。
  • @almyz125 在下面的讨论中查看我的回复。片段替换转换只是一个愚蠢的错误。

标签: android navigation-drawer


【解决方案1】:

您需要将 alpha 添加到您的 ListView。 使用您的 XML 并使用 alpha 属性扩展 ListView 定义。

<ListView
     android:id="@+id/left_drawer"
     android:layout_width="240dp"
     android:layout_height="match_parent"
     android:layout_gravity="start"
     android:choiceMode="singleChoice"
     android:divider="@android:color/transparent"
     android:dividerHeight="0dp"
     android:background="#111111"
     android:alpha="255"
/>

【讨论】:

    【解决方案2】:

    在你的代码中使用它。 获取抽屉视图的背景并为其设置 alpha。

      Drawable background = drawerView_.getBackground();
      background.setAlpha(255);
    

    【讨论】:

      【解决方案3】:

      感谢所有试图回答我问题的人。 很抱歉,问题出在代码中的片段初始化。

      所以我尝试将片段添加为新实例,但它应该只是替换布局中已定义的片段,如下所示:

      android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
      FeedFragment fragment = new FeedFragment();
      fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
      

      现在一切正常。

      【讨论】:

      • 所以你的问题不是关于抽屉的透明度,而是关于片段相互重叠
      【解决方案4】:

      要使导航抽屉透明,您必须首先在 res/values/colors.xml 文件中定义抽屉的颜色。 如:&lt;color name="navigationDrawerNew"&gt;#7ba9530c&lt;/color&gt;

      现在,在这条线的左侧,您选择的颜色将显示在一个小方块中。

      See image for example

      点击它。它会弹出一个颜色选择器。在颜色选择器的底部,有一个滑块可让您调整透明度。调整它以获得所需的透明度。 现在,将此颜色设置为 xml 文件中导航抽屉的列表视图的背景颜色。列表视图看起来几乎是这样的:-

      <ListView
          android:id="@+id/navList"
          android:layout_width="200dp"
          android:layout_height="match_parent"
          android:layout_gravity="left|start"
          android:background="@color/navigationDrawerNew" />
      

      【讨论】:

        【解决方案5】:

        将抽屉的背景(在链接的演示中是一个 ListView)设置为纯色/drawable

        【讨论】:

          【解决方案6】:

          默认情况下不会像这样透明。您必须在活动布局文件中设置它。将android.support.v4.widget.DrawerLayout标签中的android:background改为纯色

          【讨论】:

          • 用布局更新了我的问题,我已经尝试改变它。
          • 在我看来,内容以某种方式位于您的抽屉布局之上,而不是抽屉是透明的。您发布的xml是整个文件吗? android.support.v4.DrawerLayout 元素必须是布局文件中的第一件事
          • DrawerLayout 作为布局文件中的第一件事 - 是的,例如。在这里没有帮助,抱歉。
          【解决方案7】:

          您的AdaptergetView(LayoutInflater inflater, ..., ...)?

          ListView 膨胀了一些布局,这个布局似乎是透明的,而不是抽屉。 你的 Drawer 的背景颜色变成了 ListView 项目的颜色。

          您会注意到抽屉的第二部分比物品要暗一些。

          【讨论】:

          • 我在这里根本不使用NavigationDrawerAdapter - 例如developer.android.com/training/implementing-navigation/…
          • 如您所见,您实际上有一个适配器。你怎么称呼它取决于你。 mDrawerList.setAdapter(new ArrayAdapter&lt;String&gt;(this, R.layout.drawer_list_item/*this is the layout that your ListView inflates*/, mPlanetTitles)); 看看这个布局并检查它的背景。
          【解决方案8】:

          你只需要在代码中为 DrawerListView 设置BackgroundColor,如下所示:

          mDrawerListView.setBackgroundColor(Color.LTGRAY);
          

          这对我有用。无需添加Alpha值。

          【讨论】:

            【解决方案9】:

            你在java文件中添加ListView的背景颜色然后它就可以正常工作了,

            listview.setBackgroundColor(Color.WHITE);

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-11-15
              • 1970-01-01
              • 1970-01-01
              • 2014-01-05
              相关资源
              最近更新 更多