[英]android setText not working for custom title bar layout
我為我的浮動活動制作了一個自定義標題欄。 現在我想以編程方式在我的自定義標題中更改TextView的文本,但無法這樣做。 我可以通過xml更改文本,但我希望它在代碼中執行。
這里是label.java(浮動活動)的代碼,它沒有更新標題欄中的textView
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 code
myTitle.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"
/>
你這樣做是錯的。 你必須這樣做:
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)
將自定義標題布局擴展到Activity的內容區域,這當然可以讓您抓取TextView
因為您將其充氣到容器中,但之后您告訴它要充氣您的自定義標題進入窗口,它膨脹了一個完全不同的TextView,它實際上是你想要的。 然后用setContentView(R.layout.label)
覆蓋Activity的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.