Android NavigationDrawer transparency

青春壹個敷衍的年華 提交于 2020-01-04 02:03:45

问题


I’ve implemented NavigationDrawer with android-support-v4 as per this tutorial

My result is on this screenshot.

The question is how to remove or configure these transparency on Drawer which looks awful with background text.

UPDATE: here is my layout configuration, with background been set to dark

<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>

回答1:


You need to add alpha to your ListView. Use your XML and extend the ListView definition with alpha-attribute.

<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:


Use this in you code. Get the background of the drawer view and set alpha for it.

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



回答3:


To make navigation drawer transparent, you must first define the color for your drawer in res/values/colors.xml file. Eg:<color name="navigationDrawerNew">#7ba9530c</color>

Now, on the left side of this line, the color that you have chosen will be displayed in a small square.

See image for example

Click on it. It will bring up a color chooser. In the color chooser, at the bottom, there is a slider which allow you to adjust transparency. Adjust it to get the required transparency. Now, set this color as background color for the list view for navigation drawer in xml file. The list view looks almost like this:-

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



回答4:


Set the Background of the Drawer (Which in the linked demo is a ListView) to a solid color/drawable




回答5:


It doesn't default to be transparent like this. You must have it set in your activities layout file. Change the android:background in the android.support.v4.widget.DrawerLayout tag to be a solid colour




回答6:


Thanks everyone who tried to answer my question. I'm sorry the trouble was in Fragment initialization in code.

So I've tried to add fragment as new instance, but it should be just a replacement of already defined fragment in layout as follows:

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

now everything works fine.




回答7:


What's the layout that your Adapter inflates in the getView(LayoutInflater inflater, ..., ...) ?

The ListView inflates some layout and this one seems to be transparent, not the drawer. The background color of your Drawer becomes the color of ListView's items.

You can notice that the second part of your drawer is a little bit darker then the items.




回答8:


You only need to setBackgroundColor for DrawerListView in the code like this:

mDrawerListView.setBackgroundColor(Color.LTGRAY);

This is work for me. No need to add Alpha value.




回答9:


You add background color of ListView in java file then it works correctly,

listview.setBackgroundColor(Color.WHITE);



来源:https://stackoverflow.com/questions/20704700/android-navigationdrawer-transparency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!