2011-08-24 64 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