简体   繁体   English

无法在Android中将背景设置为完全透明

[英]Cannot able to set background fully transparent in android

This is the code I have used. 这是我使用的代码。 My MainActivity class 我的MainActivity课程

public class MainActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void broadcastIntent(View view){
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
    LayoutInflater inflater = MainActivity.this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.demo, null);
    dialogBuilder.setView(dialogView);
    final AlertDialog alertDialog = dialogBuilder.create();
    Window window = alertDialog.getWindow();
    window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
    window.setGravity(Gravity.CENTER);
    alertDialog.show();
  } 
 }

My dialog layout 我的对话框布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#00000000"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="150dp"
    android:layout_height="100dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">

    <TextView
        android:id="@+id/txtJioMoney"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="First"
        android:textColor="#3F51B5"
        android:textSize="18sp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second"
        android:textColor="#3F51B5"
        android:id="@+id/txtCashOnPick"
        android:gravity="center"
        android:textSize="18sp"
        android:layout_marginBottom="20dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

      </RelativeLayout>
   </RelativeLayout>

My main layout 我的主要布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button2"
    android:text="Broadcast Intent"
    android:onClick="broadcastIntent"
    android:background="@color/colorPrimary"
    android:layout_below="@+id/textView1"
    android:textColor="@android:color/white"
    android:padding="10dp"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="103dp" />

  </RelativeLayout>

I am using the AppCompatActivity so I cannot able to use @android:style/Theme.Translucent in my manifest. 我正在使用AppCompatActivity,因此无法在清单中使用@android:style / Theme.Translucent。 I want my dialog to be fully transparent I don't need that white background. 我希望我的对话框是完全透明的,不需要那种白色背景。

样本截图

There has been many answers to this question, next time try to search on stackoverflow or google before you post your question. 这个问题有很多答案,下次在发布问题之前尝试在stackoverflow或google上进行搜索。

set Transparent background to alertdialog in android 将透明背景设置为android中的alertdialog

Dialog with transparent background in Android Android中具有透明背景的对话框

android AlertDialog with transparent background 具有透明背景的android AlertDialog

Additionally, instead of using the xml style, you could also try to use .setAlpha(0) on your Relative layout programmatically. 此外,除了使用xml样式外,您还可以尝试在程序的相对布局上使用.setAlpha(0)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM