Android更改操作栏图标位置

问题描述 投票:1回答:2

我已经创建了如下所示的操作栏。

enter image description here

程序语言是RTL。我使用以下代码更改了它:

@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);
    }
}

并且在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);

黄色图标是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>

如何使黄色图标显示在操作栏的左侧?

android android-toolbar
2个回答
1
投票

为操作栏添加自定义布局,

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

toolbar.addView(mCustomView);

0
投票

更改

android:gravity="left"

with

android:gravity="end"
© www.soinside.com 2019 - 2024. All rights reserved.