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

2024-05-06

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

@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);
    ...
}

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

UPD.好吧,我宣布

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

在活动和

    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);

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

    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(...);


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

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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在fragment onCreateView中自定义android标题栏 的相关文章

随机推荐