Android canvas not drawing on rtl

萝らか妹 提交于 2019-12-11 09:47:33

问题


I have the following view class:

public class MyHorizontalViewIndicator extends View{

     protected synchronized void onDraw(Canvas canvas) {
           super.onDraw(canvas);
           Paint paint = new Paint();
           paint.setTextSize(30);
           canvas.drawText("Test",90, 90, paint);
     }
}



 <com.my.myview.MyHorizontalViewIndicator
    android:id="@+id/view_indicator"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layoutDirection="inherit"/>

When the language is English, (LTR) the test message appears fine, however, when I change the language to Arabic (RTL) the canvas is not shown.


回答1:


In Manifest.xml have you added android:supportsRtl

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Change this line canvas.drawText("Test",90, 90, paint); to

canvas.drawText("Test",bound.top-100, bound.left-100, paint);

try the negative value for the drawtext

and also change and try the layoutDirection=LOCALE in xml




回答2:


you can add this code :

 layoutDirection = LayoutDirection.LTR


来源:https://stackoverflow.com/questions/46928929/android-canvas-not-drawing-on-rtl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!