Android : Navigation button show wrong side when l

2019-05-02 09:24发布

I asked this question before (here) but nobody answered so I ask It a little bit simpler. The problem is when I change the layout direction to RTL (in xml file : android:layoutDirection="rtl" or programmatically :

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
            getWindows().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        }

doesn't matter) the navigation icon remain LTR

enter image description here

How can I fix this issue?

To be more specific the arrow should point at right!

7条回答
啃猪蹄的小仙女
2楼-- · 2019-05-02 10:14

In the Activity Class --> Add these lines :

onCreate(){
    ActionBar ab = getSupportActionBar();    
    // Enable the Up button    
    ab.setDisplayHomeAsUpEnabled(true);
    ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_arrow_back_black_24dp, getApplicationContext().getTheme()));

    }
@Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent login = new Intent(LoginActivity.this, MainActivity.class);
        login.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(login);
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        onBackPressed();
        return true;
    } else {
        return super.onOptionsItemSelected(item);
    }
}

Write a custom drawable using File--> New--> VectorAsset https://developer.android.com/studio/write/vector-asset-studio.html Choose the arrow accordingly and Enable the field -->(Enable the auto mirroring for RTL Layout)

Auto-Mirror Property checked

I have changed the color of Arrow to be White, You can choose your own color

FileName: ic_arrow_back_black_24dp.xml in drawable folder

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:autoMirrored="true"
    android:viewportHeight="24.0"
    android:viewportWidth="24.0">
    <path
        android:fillColor="#00000000"
        android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z" />
</vector>

After changing the Locale to Arabic - which changes the entire Layout from RTL to LTR

查看更多
登录 后发表回答