63

Tôi cần tạo hộp thoại trên một đoạn (chiếm toàn bộ màn hình). Hộp thoại cần phải là một hộp thoại nổi sẽ được định vị trên mảnh vỡ với mảnh bị tối ra bên ngoài đoạn đó.Không thể làm cho DialogFragment tùy chỉnh trong suốt trên Fragment

Đối với hộp thoại tùy chỉnh, tôi có một linearLayout có cạnh cong, bất kể tôi làm gì, hộp thoại có viền đen ở tất cả các bên (rất nhỏ). Tôi đã thử tất cả mọi thứ để làm cho nó minh bạch và đi xa (vì vậy mà tất cả các hộp thoại chỉ là tuyến tính bố cục - hộp cong)

Đối với DialogFragment, đây là những gì tôi có cho onCreateView

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
    LinearLayout layout =(LinearLayout)inflater.inflate(R.layout.custom_dialog, null); 
    LinearLayout item = (LinearLayout)layout.findViewById(R.id.display_item); 
    populateItemData(item, inflater); 
    return layout; 
} 

custom_dialog chỉ là một LinearLayout có android: backgroung thiết lập để # 000000

Đây là phong cách của tôi cho tùy chỉnh Dialog

<style name="CustomDialog" parent="android:style/Theme.Dialog"> 
    <item name="android:windowBackground">@null</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowIsFloating">true</item> 
    <item name="android:windowIsTranslucent">true</item> 
    <item name="android:alwaysDrawnWithCache">false</item> 
    <item name="android:windowContentOverlay">@null</item> 
</style> 

tôi đã thử tất cả các loại kết hợp trong phong cách này (từ những gì tôi đã nhìn thấy trực tuyến) và tôi không thể thoát khỏi viền đen khó chịu đó, tôi có thể tô màu trắng hoặc bất kỳ màu nào khác nếu tôi đặt nền LinearLayout đó thành bất kỳ thứ gì khác ngoài # 000000 ...

tôi đã dành 3-4 giờ theo nghĩa đen về vấn đề này, tôi hy vọng ai đó có thể giúp đỡ ...

Trả lời

206

Hãy thử

getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

trong DialogFragment 's onCreateView

+3

bạn cũng có thể muốn xóa một nửa nền đen trong suốt (mờ), kiểm tra câu trả lời này: http://stackoverflow.com/a/33800422/2115904 –

+0

Có xu hướng có ý nghĩa hơn khi được viết dưới dạng: 'getDialog(). getWindow(). setBackgroundDrawable (new ColorDrawable (Color.TRANSPARENT)); ' – IcyFlame

+2

Xóa tất cả các lề cũng .. Hộp thoại được mở rộng thành toàn bộ chiều rộng. – Uday

1

của bạn Hãy thử điều này nếu bạn muốn:

public TransparentDialog() 
{ 
    super(); 
    setStyle(STYLE_NO_FRAME, R.style.AppTheme); 
} 
19

Hãy thử điều này (How to I create a 100% custom DialogFragment) tác phẩm này với thoại

Dialog dialog = new Dialog(getActivity()); 

    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); 

    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  

     // layout to display 
    dialog.setContentView(R.layout.add_edit); 

    // set color transpartent 
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

    dialog.show(); 
3

Những người đang sử dụng AlertDialog xây dựng trong onCreateDialog thay vì onCreateView có thể gán chủ đề như sau mã. Có thể tìm thấy bộ chủ đề hoàn chỉnh từ R.style. Đừng quên rằng một số người trong số họ được hỗ trợ gần đây và không có sẵn trên điện thoại thiết bị cũ.

@Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Translucent); 
     View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_album, null); 
     builder.setView(view); 

     return builder.create(); 
    } 
+0

Xin cảm ơn, Đây là những gì tôi đang tìm kiếm. –

7

Trong onActivityCreated

getDialog().getWindow().getAttributes().alpha = 0.9f; // An alpha value to apply to this entire window. An alpha of 1.0 means fully opaque and 0.0 means fully transparent 

cho DialogFragment suốt

5

Đối với sử dụng hoàn toàn minh bạch: setStyle (DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

Đối với nền tùy chỉnh -Tạo một file phong cách bạn đánh giá cao thư mục (giá trị/style.xml) và sử dụng nó: setStyle (DialogFragment.STYLE_NO_FRAME, yourpackagename.R.style.YOURE_CUSTOM_STYLE);

trong hồ sơ phong cách của bạn ghi đè lên atribute: android: windowBackground@ màu/DialogBackgroundBlackSemiTransparent

5

Đặt chủ đề của bạn như thế này làm việc cho tôi

<style name="MyDialog" parent="Base.Theme.AppCompat.Light.Dialog"> 
    <item name="android:windowBackground">@android:color/transparent</item> 
</style> 

Và trong hộp thoại của bạn đoạn thiết lập như thế này

public class Progress extends DialogFragment { 


int style = DialogFragment.STYLE_NO_TITLE; 
int theme = R.style.MyDialog; 

public Progress() { 
} 

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setStyle(style, theme); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.progress, container, false); 
} 
} 
0
<style name="BaseDialogTheme" parent="Base.Theme.AppCompat.Light.Dialog"> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
    <item name="colorControlNormal">@color/colorAccent</item> 
    <item name="colorControlActivated">@color/colorAccent</item> 

    <item name="android:windowFullscreen">true</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowBackground">@android:color/transparent</item> 
    <item name="android:colorBackgroundCacheHint">@null</item> 
    <item name="android:colorBackground">@android:color/transparent</item> 
    <item name="android:windowIsTranslucent">true</item> 


    <item name="android:windowIsFloating">true</item> 
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> 
    <item name="android:windowActionModeOverlay">false</item> 
    <item name="android:windowCloseOnTouchOutside">true</item> 
    <item name="android:backgroundDimAmount">.00</item>//this line is changed alpha from 1.0 to 0.0(full transparent) 

</style> 



@Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setStyle(STYLE_NO_FRAME, R.style.BaseDialogTheme); 
    } 
Các vấn đề liên quan