2011-08-24 126 views
1

我试图通过典型的方法来改变标题栏的看法:如何自定义Android的标题栏的片段onCreateView

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

回答

1

会人为增加一全新的视图,并试图找到进度条/的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); 
+0

现货。当您设置自定义标题时,Window对象正在为您自定义标题。你只需要引用膨胀的视图并填充它们。 –

+0

看看更新 – LackOfKnowledge