jul*_*jul 41 android background android-dialogfragment android-styles
我正在建立一个习惯DialogFragment
.对话框布局设置为my_dialog.xml
,但如何修改对话框周围的颜色(透明灰色)?
my_dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<TextView
android:id="@+id/hello"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_orange_light"
android:gravity="center"
android:text="hello" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
MyDialogFragment.java
public class MyDialogFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_dialog, container);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return view;
}
}
Run Code Online (Sandbox Code Playgroud)
jul*_*jul 52
我必须在对话框样式中设置android:windowIsFloating
为false和android:windowBackground
我的自定义颜色:
styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@color/orange_transparent</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:gravity">center</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
MyDialogFragment
public class MyDialogFragment extends DialogFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);
}
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*nik 27
我想要一个透明的背景DialogFragment
,在代码中,这很好用:
@Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
window.setBackgroundDrawableResource(android.R.color.transparent);
}
Run Code Online (Sandbox Code Playgroud)
当然,您可以指定任何颜色或Drawable
使用setBackgroundDrawable()
或setBackgroundDrawableResource()
.
此作品至少在onStart()
,但不是onCreate()
必然的,而不是 onCreateView()
,它似乎.
也就是说,在大多数情况下,使用样式在XML中执行此操作可能更为清晰:
<style name="MyDialogStyle" parent="@android:style/Theme.Holo.Light.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
Dir*_*irk 21
要获得完全透明的对话框,可以在onCreateView
下面进行设置
setBackgroundDrawable
至 Color.TRANSPARENT
setDimAmount
至 0
请参阅此处的代码示例
public class TextEditor extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_text_editor, container);
// make dialog itself transparent
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// remove background dim
getDialog().getWindow().setDimAmount(0);
//[add more custom code here...]
return view;
}
}
Run Code Online (Sandbox Code Playgroud)
Blu*_*ell 14
我发现我只需要这样做:
<style name="MyDialog" parent="@android:style/Theme.Dialog">
<!-- other attributes -->
<item name="android:backgroundDimEnabled">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
62707 次 |
最近记录: |