动态更改popupWindow可绘制背景颜色

时间:2016-08-24 10:54:43

标签: android xml

我有一个线性布局,其背景是如下的形状

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

但没有任何改变,如何改变为可绘制背景的颜色?

1 个答案:

答案 0 :(得分:0)

它的工作原理是将drawable的值分配给View对象而不是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);