如何在android中动态设置自定义标题栏TextView值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2416844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:47:18  来源:igfitidea点击:

how to set custom title bar TextView Value dynamically in android?

androidlayoutandroid-activity

提问by UMAR

friends,

朋友们,

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

我使用以下带有代码的 titlebar.xml 文件创建了自定义标题栏

<?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" 
   /> 

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

和 java 代码以在每个活动上显示自定义标题栏。

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


}

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

现在我想在每个活动中动态设置 textview 值,任何人都可以指导我如何实现这一目标?

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.

在这里使用 findviewbyid 我没有得到该 textview 的参考来设置值,因为主布局不包含任何具有这样名称的文本框,但 mytitle。

any help would be appriciated.

任何帮助将被appriciated。

采纳答案by Diego Torres Milano

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);
    }


}

回答by cavneb

SDK 2.1+

SDK 2.1+

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.main);
    setTitle("========= NEW TITLE ==========");
}

回答by Dimitar Dimitrov

Have you tried the setTitle()method of your Activity?

你试过你的setTitle()方法Activity吗?

回答by pengwang

my_title.xml

my_title.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/header"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_width="fill_parent"
    android:background="#d4e9a9">

    <ImageView android:src="@drawable/jetpack"
        android:layout_width="wrap_content" android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" android:id="@+id/back"
        android:layout_height="wrap_content" android:layout_alignParentTop="true" />

    <TextView android:id="@+id/title" android:layout_width="wrap_content"
        android:gravity="center_vertical" android:textSize="20px"
        android:textColor="#ffffff" android:layout_alignParentRight="true"
        android:text="New Title" android:background="#a5c639"
        android:layout_height="wrap_content" android:layout_alignParentTop="true"
        android:padding="9dip" android:layout_margin="5dip" />
</RelativeLayout>

Code:

代码:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);      
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);      

((TextView)findViewById(R.id.title)).setText("gradient shadow");

findViewById(R.id.back).setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        ((TextView)findViewById(R.id.title)).setText("loce");
    }
});

Because custom title default is fixed you should write yourself a theme:

因为自定义标题默认是固定的,所以你应该自己写一个主题:

<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/MyTheme">

回答by ccheneson

Try the following:

请尝试以下操作:

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);        
     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
     setContentView(R.layout.main);
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
     TextView mytitletext = (TextView) findViewById(R.id.myTitle);
     mytitletext.setText("========= NEW TITLE ==========");
}

You can use the hierarchyviewerto see if your view is accessible (you will see its id in the hierarchy graph)

您可以使用hierarchyviewer来查看您的视图是否可访问(您将在层次结构图中看到它的 id)