Android RTL(Right-To-Left)適配

1、使用 Start、End代替Left、Right

如:

        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
替換為一下:
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:paddingStart="8dp"
        android:paddingEnd="8dp"
2、TextView增加屬性android:textDirection="locale"

如:

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="小明"
        android:textDirection="locale"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
3、ImageView圖片資源使用android:autoMirrored="true"修飾

如:
\main\res\drawable\bitmap_icon.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:autoMirrored="true"
    android:src="@drawable/icon">
</bitmap>

ImageView引用被bitmap修飾過的圖片資源

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/bitmap_icon" />
4、漸變背景適配使用drawable-ldrtl資源

LTR : ...\main\res\drawable\bg_gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:autoMirrored="true"
    android:shape="rectangle">

    <gradient
        android:angle="0"
        android:endColor="#00CD00"
        android:centerColor="#FFFFFF"
        android:startColor="#CD0000" />

    <corners android:radius="@dimen/dp12" />

</shape>

RTL: ...\main\res\drawable-ldrtl\bg_gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:autoMirrored="true"
    android:shape="rectangle">

    <gradient
        android:angle="0"
        android:endColor="#CD0000"
        android:centerColor="#FFFFFF"
        android:startColor="#00CD00" />

    <corners android:radius="@dimen/dp12" />

</shape>
5、代碼中動(dòng)態(tài)計(jì)算位置進(jìn)行適配
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容