[英]custom title bar is not working android
嗨,我已經設置了一個自定義標題欄,但是應用程序崩潰了,並且LogCat中沒有出現任何錯誤日志,我快要瘋了。 這是一些代碼,您可以請專家查看出什么問題嗎?
boolean isCustomTitleSupported;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isCustomTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.about);
customizeTitleBar("My Title");
public void customizeTitleBar(String title){
if(isCustomTitleSupported){
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitlebar);
TextView customTitleText = (TextView)findViewById(R.id.customtitle);
customTitleText.setText(title);
customTitleText.setTextColor(Color.WHITE);
}
}
customtitlebar.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/customtitle"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textStyle="bold"
android:padding="3px"
/>
</LinearLayout>
一些幫助將不勝感激
謝謝!!
編輯:我注意到我沒有擴展Activity,但是BaseActivity是我創建的超類,以便在我所有的活動中都可以使用菜單。 所以我改回去擴展Activity,它可以正常工作,但這是一個問題,因為我也需要菜單。 有什么技巧可以讓我繼續擴展BaseActivity甚至使標題欄起作用嗎?
在設置內容視圖之前,您是否嘗試過設置標題欄?
customizeTitleBar("My Title");
setContentView(R.layout.about);
您試圖從錯誤的布局中提取自customtitle
TextView。 使用findViewById
它默認為當前活動的布局,並將其設置為R.layout.about
。 您需要使用布局充氣器來充氣R.layout.customtitlebar
,然后從中調用findViewById
(因為customtitle
視圖位於customtitlebar
布局中)。
像這樣:
View view = getLayoutInflater().inflate(R.layout.customtitlebar);
TextView customTitleText = (TextView)view.findViewById(R.id.customtitle);
現在好像工作了一半,但是沒有設置標題,我的意思是我得到的標題欄是空的,這就是我得到的:
關於.java
public class About extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customizeTitleBar("APP TITLE");
setContentView(R.layout.about);
}
}
BaseActivity.java
public class BaseActivity extends Activity {
boolean isCustomTitleSupported;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isCustomTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
customizeTitleBar("MY TITLE");
}
public void customizeTitleBar(String title){
if(isCustomTitleSupported){
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitlebar);
View view = getLayoutInflater().inflate(R.layout.customtitlebar, null);
TextView customTitleText = (TextView)view.findViewById(R.id.customtitle);
customTitleText.setText(title);
customTitleText.setTextColor(Color.WHITE);
}
}
}
請求windowfeature-> setContentView->自定義標題
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Boolean customTitle = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.listviewoffers);
customTitle(R.string.dashboard_offers, 0, R.id.listViewTitle, customTitle);
這是我的onCreate的第一行。 customTitle()在我的超類中。
public void customTitle(int middle, int right, int altTitle, Boolean customTitle) {
if (customTitle) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebarlayout);
TextView titleMiddle = (TextView) findViewById(R.id.middleTitleBar);
titleMiddle.setText(getResources().getString(middle));
TextView titleRight = (TextView) findViewById(R.id.rightTitleBar);
if (right != 0) {
titleRight.setText(getResources().getString(right));
} else {
titleRight.setVisibility(View.GONE);
}
TextView title = (TextView) findViewById(altTitle);
title.setVisibility(View.GONE);
} else {
setTitle(getResources().getString(R.string.dashboard_offers));
}
}
它是一種具有2個文本視圖的布局,一個在中間,一個在右邊。 可以將權利設置為“消失”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.