安卓导航视图透明度

4
大家好。是否有可能使导航视图透明?我有自定义布局,尝试为此布局设置50%的透明背景,导航视图抽屉布局
android:background="#80000000"

但是它并没有给出期望的结果。

有人尝试过这个吗?我会感激帮助。

4个回答

8

您可以尝试以下方法:

navigationView.getBackground().setAlpha(122);

您可以在此处设置透明度,范围从0(完全透明)到255(完全不透明)。

您还可以使用XML值alpha,它接受双精度值。

范围为0f至1f(含),其中0f表示透明,1f表示不透明:

android:alpha="0.0" 不可见

android:alpha="0.5" 半透明

android:alpha="1.0" 完全可见


1
JFYI,这使得内容也透明。 - crgarridos

4
如果你想要一种透明的颜色,可以尝试以下方法...
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
------
// you can even change only one from above to and keep the other one normally 
       navigationView.getBackground().setColorFilter(0x80000000, PorterDuff.Mode.MULTIPLY);
       headerView.getBackground().setColorFilter(0x80000000, PorterDuff.Mode.MULTIPLY);

输出:

enter image description here enter image description here

如果您需要更多关于可以应用哪些颜色代码的详细信息,请查看我的答案这里


或者如果您只想设置alpha,请使用navigationView.getBackground().setAlpha(intNumberTill256);

p.s Nav头部暗色是由于我在其XML中设置的背景颜色


太好了,+1..!作为Android标签的顶级用户,您能否回答这个简短的问题?我应该为每个文件项目拥有一个public static void main(){}方法吗? - stack
2
@stack 我会这样说,首先你为什么想要这样的方法?如果你认为这应该是在 Android 类中启动第一个方法的方式,那么我必须告诉你不是这样的。在 核心 Java 程序中,我们需要一个 main() 方法,因为在执行字节码时 JVM 会在类中搜索 main() 方法并从那里开始执行。在 Android 中,每个包都包含一个清单文件。启动点在此清单文件中指定。它从指定类的 onCreate() 方法开始执行应用程序,因此不需要 main() 方法。 - Charuක
2
@stack 阅读有关 Android 生命周期的文章 // 如果您有任何疑问,请发表评论并询问 https://dev59.com/IWoy5IYBdhLWcg3wfeIR - Charuක

3
为了让导航视图完全透明,以下方法对我有效。
android:background="@android:color/transparent"

2
你可能还需要添加 app:elevation="0dp" - Iakovos Gu

1
为了使导航栏透明,请尝试以下代码。
final Window window = getWindow();
    ObjectAnimator animator = ObjectAnimator.ofInt(window,
            "navigationBarColor", window.getNavigationBarColor(), Color.TRANSPARENT);
    animator.setEvaluator(new ArgbEvaluator());
    animator.setDuration(0);
    animator.start();

#getNavigationBarColor() 需要 API 21。 - Peter Parker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接