使用自定義透明主題 Theme.TranslucentDarkGrey 時,EditText 設置 layout_width="match_parent" 但無法填滿父佈局;移除主題後恢復正常。
windowIsFloating=true 使 Activity 視窗變為對話框樣式,內容區域不再填滿螢幕,導致 match_parent 失效。
android:Theme
└─ android:Theme.Translucent ← 正確父類:透明、非浮動、全屏
└─ android:Theme.Translucent.NoTitleBar
└─ android:Theme.Dialog
└─ android:Theme.Translucent.Dialog ← 浮動、非全屏
public class TranslucentDialogFragment extends DialogFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.Theme_Translucent_Fullscreen);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_translucent, container, false);
}
}
// 主題
優點:生命周期管理更清晰、無需處理 Activity 啟動模式、配合 Navigation Component 無縫整合。
Theme.Translucent 系,而非 Theme 或 Theme.DialogwindowIsFloating=true,除非確實需要對話框浮動效果