Add button in android action bar

后端 未结 2 477
余生分开走
余生分开走 2021-01-15 03:59

I trying to add a button beside HealthyApp, but no luck .

This is my initial code and image

  final ActionBar actionBar = getSupportActionBar();
             


        
相关标签:
2条回答
  • 2021-01-15 04:46

    Add toolbar inside your xml . and use NoActionBar in your activity theme .

    add these toolbar in your xml on top

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#131313"
        android:minHeight="?attr/actionBarSize">
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:background="#00aaaaaa"
            android:layout_gravity="right"
            android:layout_height="match_parent">
    
            <Button
                android:text="Delete"
                android:layout_width="wrap_content"
                android:background="#000"
                android:textColor="#fff"
                android:layout_height="wrap_content"
                android:textSize="16dp" />
    
        </LinearLayout>
    
    </android.support.v7.widget.Toolbar>
    

    add these theme in your activity in Menifest.xml

    android:theme="@style/Theme.AppCompat.Light.NoActionBar"

    and add these code in your activity ....

    Toolbar topToolBar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(topToolBar);
    ActionBar actionBar = getSupportActionBar();;
    actionBar.setDisplayHomeAsUpEnabled(true);
    

    Output:-

    OR

    I think it is possible with the icon only .....

    use this item in menu.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/delete"
            android:icon="@drawable/YourImage"
            app:showAsAction="always"></item>
    </menu>
    

    Note:- use android:icon="@drawable/YourImageWithTextDelete" put here your image .....

    0 讨论(0)
  • 2021-01-15 04:47

    In your Activity layout

    <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:background="@color/YourColor"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            android:theme="@style/about_toolbar"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/YourText"
                 />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/YourText"
            />
    
        </android.support.v7.widget.Toolbar>
    

    In your Activity

    public class YourActivity extends ActionBarActivity {
    
        private Toolbar toolbar;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.your_acticity_layout);
    
            // Set a Toolbar to replace the ActionBar.
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            toolbar.setTitle("");
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
           }
    
           // ... your other methods
    }
    

    In your Manifest, Activity tag

    <activity
            android:name=".YourActivity"
            android:label="@string/YourActivityString"
            android:theme="@style/YourTheme" />
    

    In your style.xml

    <style name="YourTheme" parent="Theme.AppCompat.Light.NoActionBar">
       <item name="colorPrimary">...</item> <!-- declare your styles -->
    </style>
    

    If you need a better code sample: Just download https://github.com/chrisbanes/cheesesquare , It has the example toolbar.

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