【问题标题】:How to customize android title bar in fragments onCreateView如何在片段onCreateView中自定义android标题栏
【发布时间】:2011-08-24 15:50:39
【问题描述】:

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

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

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

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

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

    【讨论】:

    • 正确。当您设置自定义标题时,Window 对象会为您扩展自定义标题。您只需要获取对膨胀视图的引用并填充它们。
    猜你喜欢
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多