我正在尝试为一个碎片设置主题。
在清单文件中设置主题无效:
android:theme="@android:style/Theme.Holo.Light"
从以前的博客看来,似乎我必须使用ContextThemeWrapper。有人可以给我一个编码示例吗?我找不到任何东西。
我正在尝试为一个碎片设置主题。
在清单文件中设置主题无效:
android:theme="@android:style/Theme.Holo.Light"
从以前的博客看来,似乎我必须使用ContextThemeWrapper。有人可以给我一个编码示例吗?我找不到任何东西。
在清单文件中设置主题通常用于Activity。
如果你想为Fragment设置主题,请在Fragment的onGetLayoutInflater()中添加以下代码:
override fun onGetLayoutInflater(savedInstanceState: Bundle?): LayoutInflater {
val inflater = super.onGetLayoutInflater(savedInstanceState)
val contextThemeWrapper: Context = ContextThemeWrapper(requireContext(), R.style.yourCustomTheme)
return inflater.cloneInContext(contextThemeWrapper)
}
Fragment的主题来自它所在的Activity。每个片段都被分配了存在的Activity的主题。
主题应用于Fragment.onCreateView方法中,您的代码在其中创建视图,这些视图实际上是使用主题的对象。
在Fragment.onCreateView中,您会得到LayoutInflater参数,它会填充视图,并且它持有用于主题的Context,实际上就是Activity。因此,您填充的视图使用了Activity的主题。
要覆盖主题,您可以调用LayoutInflater.cloneInContext,文档中提到它可用于更改主题。您可以在此处使用ContextThemeWrapper,然后使用克隆的填充器来创建片段的视图。
应用单个样式,我只使用了
getContext().getTheme().applyStyle(styleId, true);
对我来说,在片段的onCreateView()
中先于充气片段的根视图之前,它能够正常工作。
getContext()
的最小 API 版本为 23。 - vigilancerandroid:theme = "@style/myTheme"
来解决问题。例如,这将更改LinearLayout
及其内容的样式,但不会影响LinearLayout
之外的任何内容。因此,为了将整个片段装饰成任何样式,请将主题应用于其最外层父布局。 <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme = "@style/myTheme" >
<TextView
android:id="@+id/tc_buttom_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time elapsed"/>
<TextView
android:id="@+id/tc_buttom_text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="00:00:00 00"/>
</LinearLayout>
onCreateDialog
方法中使用了AlertDialog.Builder
来构建对话框,因此没有使用我链接中的答案中显示的onCreateView
方法。而且当我实例化AlertDialog.Builder
时,我使用了getActivity()
来传递上下文,其实我应该使用已实例化的ConstextThemeWrapper
。
这是我的onCreateDialog代码:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Create ContextThemeWrapper from the original Activity Context
ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(getActivity(), android.R.style.Theme_DeviceDefault_Light_Dialog);
LayoutInflater inflater = getActivity().getLayoutInflater().cloneInContext(contextThemeWrapper);
// Now take note of the parameter passed into AlertDialog.Builder constructor
AlertDialog.Builder builder = new AlertDialog.Builder(contextThemeWrapper);
View view = inflater.inflate(R.layout.set_server_dialog, null);
mEditText = (EditText) view.findViewById(R.id.txt_server);
mEditText.requestFocus(); // Show soft keyboard automatically
mEditText.setOnEditorActionListener(this);
builder.setView(view);
builder.setTitle(R.string.server_dialog);
builder.setPositiveButton(android.R.string.ok, this);
Dialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
我最初是这样实例化AlertDialog.Builder
的:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
我将其更改为:
AlertDialog.Builder builder = new AlertDialog.Builder(contextThemeWrapper);
这个更改后,片段对话框使用了正确的主题进行显示。因此,如果其他人也遇到类似的问题并正在使用AlertDialog.Builder
,请检查传递给构建器的上下文。希望这可以帮助! :)
android:minSdkVersion="11"
。这可能是David的示例无法正常工作的原因。<application>
标签设置android:theme="@android:style/Theme.Holo.Light"
属性,而不是<activity>
标签。ContextThemeWrapper()
时获取上下文的方式。如果您使用类似getActivity().getApplicationContext()
的东西,请将其替换为getActivity()
。@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// create ContextThemeWrapper from the original Activity Context with the custom theme
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme);
// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
// inflate the layout using the cloned inflater, not default inflater
return localInflater.inflate(R.layout.yourLayout, null, false);
}
inflater.context.setTheme(R.style.yourCustomTheme)
创建一个Java类,然后在onCreate方法中使用你想要更改主题的布局。然后像平常一样在清单文件中提到它。
onCreateView()
或onAttach()
之前添加以下代码即可。getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
getActivity().getWindow().setStatusBarColor(Color.TRANSPARENT);
如果您想设置透明状态栏,则将根布局的fitsSystemWindows
属性设置为false。
android:fitsSystemWindows="false"