开发者

custom title bar is not working android

Hi i have set up a custom title bar but the app crashes and i don't get any error log in LogCat, i'm going crazy. Here's is some code, can you experts see what's wrong?

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>

some help will be appreciated

Thanks!!

EDIT: i noticed that i wasn't extending Activity but BaseActivity a superclass i created to have the menu available in all of my activities. So i changed back to extend Activity and it's working but this is a problem because i need menus too. Is there any tricks so i could keep extending BaseActivity and even get the title bar to work?


Have you tried setting the title bar before setting the content view ?

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


You are trying to extract the customtitle TextView from the wrong layout. When you use findViewById it defaults to your current activity's layout, which you set to R.layout.about. You need to use a layout inflater to inflate R.layout.customtitlebar and then call findViewById from that (since the customtitle view is in the customtitlebar layout).

Something like this:

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


It seems to be half working now, but no title is being set, i mean that i get an empty title bar here's is what i got:

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);
        }
    }   
}


Request the windowfeature --> setContentView --> customize Title

    @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);

This are the first lines of my onCreate. The customTitle() is in my superclass.

    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));
    }
}

It is a layout with 2 textviews, one in the middle, one in the right. The right can be set as gone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜