2016-08-03 134 views
2

我試圖創建一個全屏(但狀態欄必須仍然可見)與自定義透明(與此答案相同https://stackoverflow.com/a/29482234)的對話框,但我的背景是不透明的。具有透明背景的DialogFragment

我浪費了2天的時間嘗試所有的解決方案,但它不會工作。我的目標是顯示一個自定義暗色對話框(而不是默認的黑色)。上面的答案看起來像我所需要的,但我無法實現它的工作。有什麼建議麼?

我的代碼:

<style name="CustomDialogTheme2" parent="@android:style/Theme.Dialog"> 
    <item name="android:windowIsFloating">false</item> 
    <item name="android:windowIsTranslucent">true</item> 
    <item name="android:windowBackground">@android:color/transparent</item> 
</style> 

佈局:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#AAFFFFFF"> 

    <RelativeLayout 
     android:id="@+id/dialog_root" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginLeft="50dp" 
     android:layout_marginRight="50dp" 
     android:background="#CCFF0000" 
     android:padding="16dp"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 


    </RelativeLayout> 

</FrameLayout> 

DialogFragment:

public class TestDialogFrag extends DialogFragment { 

    public static TestDialogFrag newInstance() { 

     Bundle args = new Bundle(); 

     TestDialogFrag fragment = new TestDialogFrag(); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CustomDialogTheme2); 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.test_dialog_frag, container, false); 
     return view; 
    } 


} 

回答

0

試試下面的,它會幫助你。

佈局文件

<?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:layout_gravity="center" 
    android:gravity="center"> 

    <LinearLayout 
     android:layout_width="260dp" 
     android:layout_height="wrap_content" 
     android:background="@color/colorWhite" 
     android:orientation="vertical"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="72dp" 
      android:layout_gravity="center" 
      android:gravity="center"> 

      <ImageView 
       android:id="@+id/dialog_universal_info_image" 
       android:layout_width="match_parent" 
       android:layout_height="72dp" 
       android:background="#ff0000" 
       android:contentDescription="Imagg" 
       android:scaleType="centerCrop" /> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="72dp" 
       android:background="#4cbdbcbc" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:text="@string/app_name" 
       android:textColor="@android:color/white" 
       android:textAppearance="?android:textAppearanceLarge" /> 
     </RelativeLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:paddingBottom="22dp" 
      android:paddingLeft="26dp" 
      android:paddingRight="26dp" 
      android:paddingTop="22dp"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"> 

       <TextView 
        android:id="@+id/dialog_info_title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="10dp" 
        android:layout_marginTop="10dp" 
        android:text="heare" 
        android:textColor="@android:color/holo_green_dark" 
        android:textAppearance="?android:textAppearanceLarge" /> 

       <TextView 
        android:id="@+id/dialog_info_text" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:maxLines="4" 
        android:text="Content is here" 
        android:textColor="@android:color/holo_green_dark" /> 
      </LinearLayout> 
     </LinearLayout> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:layout_marginLeft="22dp" 
      android:layout_marginRight="22dp" 
      android:background="@android:color/holo_blue_dark" /> 


     <TextView 
      android:id="@+id/dialog_info_ok" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:paddingBottom="10dp" 
      android:paddingTop="10dp" 
      android:text="OK" 
      android:textColor="@android:color/holo_green_dark" 
      android:textSize="14sp" /> 
    </LinearLayout> 

</RelativeLayout> 

Java文件

public class DialogUtilsInfo { 

     public Activity mDialogUniversalInfoActivity; 
     private Dialog mDialog; 

     private TextView mDiaappicon_48KButton; 

     public DialogUtilsInfo(Activity mDialogUniversalActivity) { 
      this.mDialogUniversalInfoActivity = mDialogUniversalActivity; 
     } 

     public void showDialog(String content) { 
      if (mDialog == null) { 
       mDialog = new Dialog(mDialogUniversalInfoActivity, R.style.CustomDialogTheme); 
      } 
      mDialog.setContentView(R.layout.dialog_info); 
      mDialog.setCancelable(true); 
      mDialog.show(); 
      mDiaappicon_48KButton = (TextView) mDialog.findViewById(R.id.dialog_info_ok); 

      initDialogButtons1(); 
     } 


     private void initDialogButtons1() { 

      mDiaappicon_48KButton.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View view) { 
        mDialog.dismiss(); 
       } 
      }); 
     } 

     public void dismissDialog() { 
      mDialog.dismiss(); 
     } 
    } 
+0

你有沒有嘗試過呢? @ fanboy555 – MathaN