我已经为我的浮动活动创建了一个自定义标题栏。现在,我想以编程方式更改自定义标题中的TextView文本,但无法执行此操作。我可以通过xml更改文本,但我希望它在代码中完成。
以下是不更新标题栏中的textView的label.java(浮动活动)的代码
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.my_title);
TextView label = (TextView)findViewById(R.id.myTitle);
label.setText("Label here code");//not working
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
setContentView(R.layout.label);// as i need this layout for rest of activity
//rest of codemyTitle.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myTitle"
android:text="Label here"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@android:color/white"
/> 发布于 2012-08-08 05:50:14
你做错了。你必须这样做:
public class CustomTitleActivity extends Activity {
private TextView title;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.label);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
title = (TextView) findViewById(R.id.title);
title.setText("My custom title");
}
}您试图使用setContentView(R.layout.my_title)将您的自定义标题布局膨胀到活动的内容区域中,这当然允许您获取TextView,因为您将其膨胀到容器中,但随后您告诉它将您的自定义标题膨胀到窗口中,这将膨胀一个完全不同的TextView,这实际上就是您想要的。然后,您使用setContentView(R.layout.label)覆盖了活动的内容。
https://stackoverflow.com/questions/11854338
复制相似问题