Android: Adding button to custom title bar

后端 未结 2 1830
情深已故
情深已故 2021-01-06 09:58

I have created a custom title bar as shown in this example

http://staticallytyped.wordpress.com/2011/03/18/android-dynamic-and-custom-title-bars/

\"A custom

相关标签:
2条回答
  • 2021-01-06 10:35

    Here's one approach to make sure the button(s) remain in the title bar:

    How to Create Custom Window Title in Android

    Essentially, wrap the android Activity class, and then extend from that new class.

    0 讨论(0)
  • 2021-01-06 10:39

    First of all, thanks for the link to my blog. Second, let me see if I can't answer that for you. One of the reasons you're having trouble adding another button when you want to add a button is that in that example I left you with no way of retrieving the Title Bar View through the usual channels. Hence, let's fix it (and potentially let me write another blog post this coming weekend.)

    Starting with the xml file, add an id attribute:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:id="@+id/title_complex">
        <!-- Stuff -->
    </LinearLayout>
    

    and here's code to show you how to get that button in there within your Activity (you'll have to add all the flair later):

    LinearLayout layout = (LinearLayout) getWindow().findViewById(R.id.title_complex);
    layout.addView(new Button(this));
    

    and if you take a look, there's a non-descript button in the Title Bar (like I said, you'll have to add your own flair):

    Title Bar with another button

    Due note, however, that I can't guarantee that the button will remain or won't remain on subsequent Activities. I haven't investigated it yet but this should get you started. And if you have any more questions, feel free to ask them here (more eyeballs) or on my blog.

    0 讨论(0)
提交回复
热议问题