【问题标题】:To set background of Custom Layout with an ImageView to Transparent使用 ImageView 将自定义布局的背景设置为透明
【发布时间】:2012-07-23 07:51:27
【问题描述】:

我正在使用 Android 开发者文档link 构建一个自定义对话框,为此我制作了一个布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rotatelayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#80000000"
>

<ImageView 
    android:id="@+id/dialogimage"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
</LinearLayout>

我正在夸大这个对话框,

AlertDialog dialog;
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(TabsActivity.this);
LayoutInflater inflator = (LayoutInflater)   getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflator.inflate(R.layout.rotate_dialog_layout, (ViewGroup) findViewById(R.id.rotatelayout));   
ImageView image = (ImageView) view.findViewById(R.id.dialogimage);
image.setOnClickListener(new RotateLockListener());
image.setBackgroundColor(Color.parseColor("#80000000"));
if(locked) 
{
    image.setImageResource(R.drawable.lock_icon);
}
else 
{
    image.setImageResource(R.drawable.unlock_icon);
}
builder.setView(view).setCancelable(true);
dialog = builder.create();
dialog.setView(view, 0, 0, 0, 0);
timerDelayRemoveDialog(time, dialog);
dialog.show();

但它仍然显示为

我已经尝试过堆栈中提供的所有帮助

Setting ImageView background to transparent,

Setting transparency by #80000000

Setting Dialog window transparency

但它们都不起作用,它仍然出现。

【问题讨论】:

  • #80000000 不对,你应该在线性布局中使用 #00000000
  • android:background="@android:color/transparent"
  • @user370305 我也用过,但它仍然是黑色的。

标签: android android-layout android-dialog


【解决方案1】:

或者您可以为此使用 Dialog 类,

Dialog dialog = new Dialog(this,
                android.R.style.Theme_Translucent_NoTitleBar);
        Window window = dialog.getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        window.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.FILL_PARENT);
        dialog.setCancelable(true);

        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(R.layout.temp);
        dialog.show();

【讨论】:

  • 你能告诉我代码中究竟是什么产生了图中的黑色背景吗?
  • 这是任何“警报”对话框的默认主题。我不知道如何从警报对话框中删除主题,但这是我用于自定义对话框的,它工作正常。
【解决方案2】:

尝试将其用作视图的样式:

<style name="Dialog_Fullscreen">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowNoTitle">true</item>
</style> 

【讨论】:

    猜你喜欢
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 2021-03-04
    • 1970-01-01
    • 2015-06-18
    • 2011-08-31
    相关资源
    最近更新 更多