Android:当布局方向为RTL时,导航按钮显示错误的一面 [英] Android : Navigation button show wrong side when layout direction is 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>
这篇关于Android:当布局方向为RTL时,导航按钮显示错误的一面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!