2016-08-24 64 views
-1

我有一個線性佈局誰的背景是一個形狀如下面更改popupWindow繪製背景顏色動態

popup_shape.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 
    <solid android:color="#ff5722"/> 
    <stroke android:width="3dip" android:color="#FFFFFF" /> 
    <corners android:radius="10dip"/> 
    <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" /> 

</shape> 

上述形狀被用作背景的下面佈局。

pause_dialog.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:background="@drawable/pupup_shape" 
    android:orientation="vertical" 
    android:weightSum="4"> 

    <TextView 
     android:id="@+id/txt_puse_scoreTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Your Score is:" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:textSize="@dimen/abc_text_size_display_1_material" 
     android:textColor="@android:color/white"/> 

    <TextView 
     android:id="@+id/txt_puse_score" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="70" 
     android:layout_gravity="center" 
     android:textAlignment="center" 
     android:layout_weight="1" 
     android:textSize="@dimen/abc_text_size_display_2_material" 
     android:textColor="@android:color/white"/> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:weightSum="2"> 

     <ImageButton 
      android:id="@+id/btn_gamePuseRsume" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:src="@mipmap/dialog_resum" 
      android:background="@android:color/transparent" 
      android:scaleType="fitCenter" 
      android:adjustViewBounds="true" 
      android:layout_margin="10dp" 
      android:layout_weight="1"/> 

     <ImageButton 
      android:id="@+id/btn_gamePuseExit" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:src="@mipmap/dialog_exit" 
      android:background="@android:color/transparent" 
      android:scaleType="fitCenter" 
      android:adjustViewBounds="true" 
      android:layout_margin="10dp" 
      android:layout_weight="1" 
      /> 
    </LinearLayout> 

</LinearLayout> 

這個佈局需要用作彈出窗口, 主要的問題,我需要改變這種佈局的背景顏色,但它不會改變, 的方式,我正在改變顏色是通過此代碼:

LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
View popupView = layoutInflater.inflate(R.layout.pause_dialog, null); 
final PopupWindow popupWindow = new PopupWindow(popupView, ActionBar.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true); 

GradientDrawable drawable = (GradientDrawable) popupView.getResources().getDrawable(R.drawable.pupup_shape); 
drawable.setColor(Color.parseColor("#34495e")); 
popupWindow.setBackgroundDrawable(drawable); 

但是沒有什麼是改變,如何改變爲可繪製背景的顏色?

回答

0

它的工作方式的drawable的值賦給視圖對象,而不是PopupWindow

LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
View popupView = layoutInflater.inflate(R.layout.pause_dialog, null); 
GradientDrawable drawable = (GradientDrawable) popupView.getResources().getDrawable(R.drawable.pupup_shape); 
drawable.setColor(Color.parseColor(arrCircleColors[colorRand])); 
popupView.setBackground(drawable); 
final PopupWindow popupWindow = new PopupWindow(popupView, ActionBar.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);