简体   繁体   中英

android add button to title bar

I am trying to add a button to title bar but I couldn't succeed. I have read many articles about this but they all explain the way that doesn't fit my situation. I am using ScrollView to make the screen scrollable. but articles suggest me to use linear layout. How can I make it scrollable and have a button on the title bar?

here is my XML

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#C0C0C0" 
android:id="@+id/sw_layout">
    
<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:id="@+id/ln_layout">
    
    <TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft=<"@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >
        .........................
        .........................
        .........................
    </TableLayout>
 </LinearLayout>
</ScrollView>
        

And in my MainActivity, I have this code....

LinearLayout ln = (LinearLayout) getWindow().findViewById(R.id.ln_layout);
Button btn = new Button(this);
btn.setText("Test");
ln.addView(btn);

But this doesn't display anything and it doesn't give me any error. Please give me an idea. how can I add the button to title bar?

make title.xml and design your title : place button etc

on activity

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

you will access to elements on title the same way you do on main layout .using findviewbyid

for avoiding some look problems

<style name="theme">
    <item name="android:windowTitleBackgroundStyle">@style/themeTitleBackground</item>
    <item name="android:windowTitleSize">65dip</item>
 
</style>
  
<style name="themeTitleBackground"> 
    <item name="android:layout_height">wrap_content</item>
       
    
</style>

inside androidmanifest

 <activity   android:theme="@style/theme" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM