问题
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