2012-11-22 31 views
2

我正在使用不同的佈局在我的應用程序中顯示不同風格的標題。問題是我無法獲得對其中所有特定Textview的引用。如果我改變文字,什麼都不會發生。如何獲得自定義標題視圖?

下面是我在做什麼:

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); 

下面的方法不起作用:

public void setTitle(String text) { 
    TextView txtTitle = (TextView) this.findViewById(R.id.txtTitle); 
    if(txtTitle !=null) { 
     txtTitleMoney.setText(text); 
     Log.d("debug", "not null"); 
    } 
} 

我沒有得到任何錯誤,所以我想我訪問了錯誤的看法。

希望有人能幫助我:)

+0

爲什麼你檢查txtTitle爲null,然後修改txtTitleMoney?你怎麼知道txtTitleMoney不是null? –

+0

你可以顯示xml佈局嗎? – everlasto

回答

5

如果你設置了相同的ID(「txtTitle」)在所有不同的標題佈局的TextView項目,那麼你的代碼應該管用。 也確保您在setContentView()之後致電setTitle()

5

試試這個

boolean customTitleSupported; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
    setContentView(R.layout.mainscreen); 

    customTitleBar(); 
} 

public void customTitleBar() { 

    if (customTitleSupported) { 
      getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, 
          R.layout.customtitlebar); 
      TextView title = (TextView) findViewById R.id.title);    
      title.setText(left);    
    } 
} 
+0

問題是我正在改變佈局。我不僅在創建時調用setFeatureInt,而且每次我的用戶都更改選項卡。 –

+0

我正在改變佈局?意味着它的意思。 –

+0

提供您已完成的代碼 –

相關問題