2016-03-01 132 views
0

有谁知道如何以编程方式变暗View's背景,如alertDialogNavigationDrawer这样做?我需要背景与ToolBar(1)和StatusBar重叠,但StatusBar的图标应该可见(2)。在我的情况下,它不应该是dialog。例如,我必须加深ButtonCustomView的背景。Android变暗背景

Example Image

回答

0

我一直在寻找这个问题的答案很长一段时间,但最后我找到了解决办法。

我研究了代码“DialogPlus”库,找出关键点。

orhanobut/dialogplus

getWindow().getDecorView();

final LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
     final View background = inflater.inflate(R.layout.frame, null); 
     final Animation a = AnimationUtils.loadAnimation(this, R.anim.in); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       ViewGroup decorView = (ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content); 
       decorView.addView(background); 
       background.startAnimation(a); 
      } 
     }); 

背景resoource文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/black" 
     android:alpha="0.5"></FrameLayout> 
</LinearLayout>