如何获得自定义标题视图?

K. *_* D. 2 java android android-layout

我使用不同的布局在我的应用程序中显示不同的样式标题.问题是我无法获得对所有这些文件中存在的特定Textview的引用.如果我改变文本,没有任何反应.

这是我在做的事情:

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
Run Code Online (Sandbox Code Playgroud)

以下方法不起作用:

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

我没有得到任何错误,所以我认为我正在访问错误的视图.

希望有人可以帮助我:)

Raj*_*ddy 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);             
    }
}
Run Code Online (Sandbox Code Playgroud)


Rot*_*tem 5

如果您txtTitleTextView所有不同的标题布局中的项目设置相同的ID(" "),那么您的代码应该有效.还要确保你调用setTitle()setContentView().