如何在android中动态设置自定义标题栏TextView值? [英] how to set custom title bar TextView Value dynamically in android?
本文介绍了如何在android中动态设置自定义标题栏TextView值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友,
我使用以下带有代码的 titlebar.xml 文件创建了自定义标题栏
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 代码.
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.
任何帮助都会得到帮助.
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);
}
}
这篇关于如何在android中动态设置自定义标题栏TextView值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文