【问题标题】:how to display a popup windows with transparent background image?如何显示具有透明背景图像的弹出窗口?
【发布时间】:2014-05-17 13:23:06
【问题描述】:

大家好,我正在制作一个应用程序,在其中显示带有背景图像的弹出窗口,

我给弹窗设置的背景图片是透明图片 但问题是当显示弹出窗口时,背景图像显示不正确....

虽然它是透明图像,但它会在图像的角落显示黑色条带..

谁能帮帮我??

PopupDemoActivity.java

包 com.demo.popupwindow.;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;  
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;

public class PopupDemoActivity extends Activity {

Button searchMenu, viewOrder;

PopupWindow popUp;
LayoutParams params;
FrameLayout layout;
// LinearLayout layout;
boolean click = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.popdemodemo);

    searchMenu = (Button) findViewById(R.id.menu);
    viewOrder = (Button) findViewById(R.id.order);
    popUp = new PopupWindow(this);

    // layout = new LinearLayout(this);
    layout = new FrameLayout(this);


    viewOrder.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            if (click) {
                popUp.showAtLocation(layout, Gravity.TOP | Gravity.RIGHT,
                        0, 0);
                popUp.update(30, 75, 500, 400);
                click = false;
            } else {
                popUp.dismiss();
                click = true;
            }

        }
    });

    // popUp.setContentView(layout);

    params = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);

    layout.setBackgroundResource(R.drawable.order_back);
    // layout.setBackgroundColor(Color.TRANSPARENT);
    popUp.setContentView(layout);
}

}

popupdemo.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white_color"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/header_lay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <Button
        android:id="@+id/menu"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:text="Search Menu"
        android:textColor="@color/white_color"
        android:textSize="25sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/order"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="30dp"
        android:text="View Order"
        android:textColor="@color/white_color"
        android:textSize="25sp"
        android:textStyle="bold" />
</RelativeLayout>

【问题讨论】:

  • 请发布您的弹出视图的布局文件。

标签: android


【解决方案1】:

Jayesh,试试这个:

popUp.setBackgroundDrawable(new ColorDrawable(
            android.graphics.Color.TRANSPARENT));

【讨论】:

  • 这是完美的解决方案,因为它不会阻止弹出窗口在外部点击时关闭
  • 你试过这个“popUp.setOutsideTouchable(true/false);” ?
【解决方案2】:

设置

popUp.setBackgroundDrawable(null);

它应该做的工作。你在后台得到的是弹出窗口的某种背景内容

【讨论】:

  • 你要做的是创建一个透明的png(例如clear.png)文件并将它放在你的drawables文件夹中,然后调用popUp.setBackgroundDrawable(R.drawable.clear);
【解决方案3】:

在您的 XML 中,您将弹出窗口的背景定义为

android:background="@color/white_color"

尝试在此处而不是在运行时应用背景图像。

android:background="@drawable/order_back"

移动上面的弹出布局更改代码(popUp.showAtLocation(layout, Gravity.TOP | Gravity.RIGHT,0, 0);之前)

【讨论】:

  • 感谢您的回答,但我需要弹出窗口的背景图像而不是我为 PopDemoActivity 设置为 setContentView(); 的布局;而且我没有更改布局,但为了显示弹出窗口,我正在创建新布局并设置背景...我没有使用现有背景.....
  • @Jayesh 你找到解决方案了吗?
  • @rana:见 vijju 的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
  • 2023-01-30
  • 1970-01-01
  • 1970-01-01
  • 2020-10-29
  • 2012-05-26
  • 1970-01-01
相关资源
最近更新 更多