简体   繁体   中英

Android change Action Bar icon position

I have created an Action bar like the following.

在此处输入图片说明

The program language is RTL. I have changed it using the following code:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void forceRTLIfSupported()
{
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
        getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
    }
}

And used the following code in onCreate() :

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setIcon(R.drawable.actionbar_icon);
    getSupportActionBar().setTitle("سیکاس");
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xff378417));
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

The yellow icon is a Drawable .

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/ic_launcher"
        android:gravity="left"
        android:top="5dp"
        android:left="8dp" />

</layer-list>

How can I make the make the Yellow Icon appear on the left side of the Action bar?

Add custom layout for the actionbar,

setSupportActionBar(toolbar);
LayoutInflater mInflater=LayoutInflater.from(context);
View mCustomView = mInflater.inflate(R.layout.toolbar_custom_view, null);

toolbar.addView(mCustomView);

Change

android:gravity="left"

with

android:gravity="end"

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