gpt4 book ai didi

android - 如何在 fragment onCreateView 中自定义 android 标题栏

转载 作者:行者123 更新时间:2023-11-30 04:34:07 24 4
gpt4 key购买 nike

我正在尝试通过典型方法更改标题栏的 View :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle args) {
...
Window window = getActivity().getWindow();
window.requestFeature(Window.FEATURE_CUSTOM_TITLE);
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.brand_update_layout);
ProgressBar progressBar = (ProgressBar) inflater.inflate(R.layout.brand_update_layout, group, false)
.findViewById(R.id.title_progress);
TextView title_Text = (TextView) inflater.inflate(R.layout.brand_update_layout, group, false)
.findViewById(R.id.title_text);
title_Text.setText(Html.fromHtml("..."));
progressBar.setVisibility(View.VISIBLE);
...
}

但是由于某些原因这段代码不起作用。怎么了?

更新。好的,我声明了

    Window window = getWindow();
window.requestFeature(Window.FEATURE_CUSTOM_TITLE);
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.brand_update_layout);

在 Activity 中

    ProgressBar progressBar = (ProgressBar)getActivity().getWindow().findViewById(R.id.title_progress);
TextView title_Text = (TextView)getActivity().getWindow().findViewById(R.id.title_text);
title_Text.setText(...);
progressBar.setVisibility(View.VISIBLE);

在 fragment 中,但是我有一个错误:

    ERROR/AndroidRuntime(12213): java.lang.NullPointerException
at com.quto.ru.CarListFragment.onCreateView(CarListFragment.java:188)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:837)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1041)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:616)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1359)

关于字符串 title_Text.setText(...);

最佳答案

您正在膨胀一个全新的 View ,并尝试使用以下代码在该新膨胀的 View 中找到进度条/ TextView :

ProgressBar progressBar = (ProgressBar) inflater.inflate(R.layout.brand_update_layout, group, false).findViewById(R.id.title_progress);
TextView title_Text = (TextView) inflater.inflate(R.layout.brand_update_layout, group, false).findViewById(R.id.title_text);

试试改成这个

ProgressBar progressBar = (ProgressBar)window.findViewById(R.id.title_progress);
TextView title_Text = (TextView)window.findViewById(R.id.title_text);

关于android - 如何在 fragment onCreateView 中自定义 android 标题栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7178461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com