自定义标题栏不能正常工作

时间:2010-12-16 17:06:16

标签: 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,但我创建了一个超类我创建的超类,以便在我的所有活动中提供菜单。所以我改回来扩展Activity并且它正在工作,但这是一个问题,因为我也需要菜单。有没有任何技巧,所以我可以继续扩展BaseActivity甚至让标题栏工作?

4 个答案:

答案 0 :(得分:3)

在设置内容视图之前,您是否尝试过设置标题栏?

customizeTitleBar("My Title");
setContentView(R.layout.about);

答案 1 :(得分:1)

您正尝试从错误的布局中提取customtitle TextView。使用findViewById时,默认为当前活动的布局,您将其设置为R.layout.about。您需要使用布局inflater来膨胀R.layout.customtitlebar,然后从中调用findViewById(因为customtitle视图位于customtitlebar布局中)。

这样的事情:

View view = getLayoutInflater().inflate(R.layout.customtitlebar);
TextView customTitleText = (TextView)view.findViewById(R.id.customtitle);

答案 2 :(得分:0)

现在似乎已经完成了一半的工作,但没有设置标题,我的意思是我得到一个空的标题栏这里是我得到的:

<强> About.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);
        }
    }   
}

答案 3 :(得分:0)

申请窗口特征 - &gt; setContentView - &gt;自定义标题

    @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个textviews的布局,一个在中间,一个在右边。权利可以设置为已经消失。