Android:当布局方向为RTL时,导航按钮显示错误的一面 [英] Android : Navigation button show wrong side when layout direction is RTL

查看:369
本文介绍了Android:当布局方向为RTL时,导航按钮显示错误的一面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾问过这个问题(此处),但没有人回答,所以我问它要简单一些. 问题是当我将布局方向更改为RTL时 (在 xml文件:android:layoutDirection="rtl" 或以编程方式:

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

没关系)导航图标保持LTR

doesn't matter) the navigation icon remain LTR

如何解决此问题?

更具体地说,箭头应指向右边!

To be more specific the arrow should point at right!

推荐答案

在活动类中-> 添加这些行:

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


使用File-> New-> VectorAsset编写自定义图形 https://developer.android.com/studio/write/vector- asset-studio.html 选择相应的箭头,然后启用字段->(为RTL布局启用自动镜像)


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)

自动镜像属性已选中

我将箭头的颜色更改为白色,您可以选择自己的颜色

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

文件名:可绘制文件夹中的ic_arrow_back_black_24dp.xml

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>

将语言环境更改为阿拉伯语后-整个布局从RTL更改到LTR

这篇关于Android:当布局方向为RTL时,导航按钮显示错误的一面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆