2015-11-07 64 views
0

我必須做一個透明的對話框,像這個圖像。我已經嘗試了許多方式,如設置背景android:background="#CCFF0000"或設置像這樣的.java文件的背景顏色在android中製作透明對話框

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");

enter image description here

+0

需要看佈局的對話框 – Bhargav

+0

好吧,我更新我的問題 –

+0

請避免嵌套佈局(壞的表演),使用單RelariveLayout,來代替。 –

回答

0

最後我解決了使用此行代碼這一問題納入onCreateView回調。

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

我發現它從here

0

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

注意: 但考慮使用默認的背景和材料設計的UI模式來compilant。

0

在你dialogFragment的onCreate,嘗試使用的setStyle()設置一個風格 @android:彩色/透明 真正