如何更改弹出布局的背景?

时间:2015-10-23 04:51:41

标签: java android xml layout popup

我创建了一个弹出窗口,并设置了一个弹出窗口背景。 我的xml文件如下。我想改变弹出窗口的背景。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/pp1" >

<WebView
    android:id="@+id/webview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="30dp"
    android:layout_marginLeft="35dp"
    android:layout_marginRight="35dp"
    android:layout_marginTop="30dp" />

我的showPopup方法如下。那么如何更改弹出窗口背景呢?我也在弹出窗口中加载了一个webview。

    private void showPopup(final Activity context, Point p) {
    int popupWidth = 720;
    int popupHeight = 380;

    // Inflate the popup_layout.xml
    LinearLayout viewGroup = (LinearLayout) context
            .findViewById(R.id.popup);
    LayoutInflater layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
    if (i == 1) {
        WebView wv = (WebView) layout.findViewById(R.id.webview1);
        wv.loadUrl("file:///android_asset/aboutus.html");
    }
    /*
     * Resources res = getResources(); Drawable drawable =
     * res.getDrawable(R.drawable.newImage); LinearLayout linearLayout =
     * (LinearLayout)findViewById(R.id.GameLayout);
     * linearLayout.setBackgroundDrawable(drawable);
     */

    // Creating the PopupWindow
    final PopupWindow popup = new PopupWindow(context);
    popup.setContentView(layout);
    popup.setWidth(popupWidth);
    popup.setHeight(popupHeight);
    popup.setFocusable(true);

    // Some offset to align the popup a bit to the right, and a bit down,
    // relative to button's position.
    int OFFSET_X = 130;
    int OFFSET_Y = 100;
    // Clear the default translucent background
    // popup.setBackgroundDrawable(new BitmapDrawable());

    // Displaying the popup at the specified location, + offsets.
    popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y
            + OFFSET_Y);

    // Getting a reference to Close button, and close the popup when
    // clicked.

}

1 个答案:

答案 0 :(得分:1)

试试这个,

从xml文件更改背景:

在xml文件中的layout标记中写入

android:background="@drawable/image">

并在可绘制文件夹中添加图片,确保图片为 .png 格式。

在运行时更改背景:

btn.setBackground(this.getResources()getDrawable(R.drawable.yellow_button));

相反,您可以使用此

编写活动名称

btn.setBackground(MainActivity.this.getResources()getDrawable(R.drawable.yellow_button));