2016-08-24 11 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> 

このレイアウトは、ポップアップウィンドウとして使用する必要が

、 私はこのレイアウトの背景色を変更する必要があるが、それは変更されません主な問題、 方法I

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

ではなくPopupWindowのViewオブジェクトにdrawableの値を割り当てることによって動作します。

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); 
関連する問題