如何动态地在Android中设置自定义标题栏TextView的价值?自定义、标题栏、价值、动态

2023-09-12 22:11:28 作者:天涯浪人

朋友,

我已经创建使用以下titlebar.xml文件code自定义标题栏

i have created custom title bar using following titlebar.xml file with code

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/myTitle" 
  android:text="This is my new title" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:textColor="@color/titletextcolor"
  android:layout_marginLeft="25px"
   android:paddingTop="3px" 
   /> 

和java code到显示器上的每个活动自定义标题栏。

and java code to display custom title bar on each activity.

@Override
    public void onCreate(Bundle savedInstanceState)
    {
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);

super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


}

现在我想在每个活动动态设置TextView的价值任何一个可以指导我怎样才能做到这一点?

now i want to set textview value dynamically in each activity can any one guide me how can i achieve this?

使用findviewbyid在这里,我不明白这TextView的设定值,因为参考 主要布局不包含这样的名字,但mytitle任何文本。

using findviewbyid here i dont get reference of that textview to set value because main layout does not contains any textbox with such a name but mytitle.

任何帮助将appriciated。

any help would be appriciated.

推荐答案

这是设置自定义标题的方式:

This is the way to set the custom title:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.main);

    if ( customTitleSupported ) {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
    }

    final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
    if ( myTitleText != null ) {
        myTitleText.setText("========= NEW TITLE ==========");
        myTitleText.setBackgroundColor(Color.GREEN);
    }


}