How to add imageview on right hand side of action bar?

后端 未结 5 1872
星月不相逢
星月不相逢 2021-01-30 05:53

I want to add image view on action bar right hand side.I tried to do that but i couldn\'t. Can anyone help me?

This is my image view xml

 

        
相关标签:
5条回答
  • 2021-01-30 06:00

    try this...

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayOptions(actionBar.getDisplayOptions()
                | ActionBar.DISPLAY_SHOW_CUSTOM);
        ImageView imageView = new ImageView(actionBar.getThemedContext());
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        imageView.setImageResource(R.drawable.adnace_search_i);
        ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
                        | Gravity.CENTER_VERTICAL);
        layoutParams.rightMargin = 40;
        imageView.setLayoutParams(layoutParams);
        actionBar.setCustomView(imageView);
    
    0 讨论(0)
  • 2021-01-30 06:04

    You don't need an ImageView for this. Using a new item using Icon for your image will be the smartest solution.

    Add this item to your menu.xml:

    <item
        android:title="@string/action_scanner"
        android:orderInCategory="79"
        android:icon="@drawable/adnace_search_i"
        android:showAsAction="ifRoom|withText" />
    

    Instantiate the menu in onCreateOptionsMenu and implement it in onOptionsItemSelected.

    0 讨论(0)
  • 2021-01-30 06:05

    Use

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowCustomEnabled(true);
    
    LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.custom_imageview, null);
    
    actionBar.setCustomView(v);
    

    Code for Custom ImageView

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout">
    
    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/adnace_search_i" />
    </RelativeLayout>
    
    0 讨论(0)
  • 2021-01-30 06:08

    Add this line to the menu.xml in the res/menu/menu.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
     <!-- Search, should appear as action button -->
     <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />
     ...
     </menu>
    

    You can see more actionBar property over android developer tutorials

    0 讨论(0)
  • 2021-01-30 06:20

    Use this layout. I just used that on API 28

    <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme.AppBarOverlay">
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay">
    
                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
    
                        <ImageView
                            android:layout_width="24dp"
                            android:layout_height="24dp"
                            android:onClick="makeToast"
                            android:layout_alignParentEnd="true"
                            android:src="@drawable/ic_flag" />
                    </RelativeLayout>
                </android.support.v7.widget.Toolbar>
    
    0 讨论(0)
提交回复
热议问题