簡體   English   中英

在Android中制作透明對話框

[英]Make a transparent Dialog in android

我必須像這樣創建一個透明的對話框。 我已經嘗試過多種方法,例如,設置背景android:background="#CCFF0000"或從.java文件設置背景顏色是這樣的

llWhole.setBackgroundColor(Color.TRANSPARENT); 這里llWhole是我的xml的父級LinearLayout

我也在xml文件中嘗試了android:background="@android:color/transparent" 我還嘗試了View.setAlpha(0.7)

不幸的是,沒有人幫助我創建此布局。 您能幫我解決這個問題嗎? 提前致謝。

我的.xml在這里

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/llWhole"
    android:alpha = "0.7"
    android:layout_gravity="center"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:alpha = "0.7"
        android:layout_gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Are You OK?"
            android:textColor="#000"
            android:paddingTop="10dp"
            android:paddingRight="10dp"
            android:paddingLeft="10dp"
            android:gravity="center"
            android:textSize="18sp"
            android:background="#FFF"/>

        <View
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:alpha = "0.7"
            android:background="#00FFFFFF" >
        </View>

        <LinearLayout
            android:paddingTop="10dp"
            android:paddingRight="10dp"
            android:paddingLeft="10dp"
            android:paddingBottom="20dp"
            android:background="#00FFFFFF"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tvOK"
                android:layout_width="55dp"
                android:layout_height="wrap_content"
                android:text="OK"
                android:textColor="#000"
                android:gravity="center"
                android:textSize="18sp"
                android:background="#F11"/>

            <TextView
                android:id="@+id/tvNo"
                android:layout_width="55dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="NO"
                android:textColor="#000"
                android:gravity="center"
                android:textSize="18sp"
                android:background="#F11"/>

        </LinearLayout>
        </LinearLayout>


</LinearLayout>

注意:

  1. 我正在使用com.android.support:appcompat-v7:23.0.1
  2. 我的對話框是android.support.v4.app.DialogFragment
  3. 我的mainActivity擴展了android.support.v7.app.AppCompatActivity; 從本活動中,我正在顯示此DialogFragment。

    MyDialogFragment dialogFragment = new MyDialogFragment(); dialogFragment.show(getSupportFragmentManager(), "my_transparent_dialog");

在此處輸入圖片說明

請嘗試myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

注意:但是請考慮使用默認背景,並與材料設計ui模式保持一致。

在您的dialogFragment onCreate中,嘗試使用setStyle(,)設置@android:color / transparent true的樣式

最后,我使用了這行代碼到onCreateView回調中解決了這個問題。

getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));

我是從這里找到的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM