右对齐(Right-to-Left)语言导致的问题(如:阿拉伯语)

http://droidyue.com/blog/2014/07/07/support-rtl-in-android/
Android支持RTL(从右向左)语言

RTL语言

    RTL语言即Right-to-left(从右向左)的语言.其书写习惯为从右向左,朝左继续.
    这里的RTL严格意义上指的是Right-to-left,top-to-bottom 从右向左,从上到下. 而汉语,日语以及朝鲜语中存在Top-to-bottom, right-to-left从上到下,从右向左的情况. 但是这些语言大多数使用从左向右的情况.
    常见的RTL语言有阿拉伯语,希伯来语等.
    LTR语言和RTL语言刚好相反,即我们常见的Left-to-right,Top-to-bottom,从左向右,从上到下,朝右继续.这种语言很常见, 代表语言为英语等.
    双向文本( Bi-Directional Text),即RTL文本和LTR文本混合的情况.比较复杂.

Android对RTL支持情况

    Android 4.1 Jelly Bean,引入了有限的在TextView和EditText支持RTL和LTR混合的双向文本的支持.
    从Android 4.2 开始,提供了全面的本地布局支持,允许镜像布局,可以同时支持RTL和LTR.

如何支持RTL
快速应用RTL镜像布局

    在manifest中的Application元素加入android:supportsRtl=“true”这个属性.

未加入android:supportsRtl=“true” 阿拉伯语(RTL)的示例.
[layout without rtl support]

加入该属性的英文(LTR)语言效果.
[rtl support english]

加入该属性的阿拉伯语(RTL)语言效果.
[rtl support arabic]

    将所有的左右(left/right)布局属性替换成开始结束(start/end)属性. 如下述代码,使用paddingLeft



    

<TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/tvOne"
      android:paddingLeft="100dp"
      android:background="#ccefc8"
      android:text="@string/share"
      />

阿拉伯语下的效果图为
[paddingLeft Arabic] 如上面所说,RTL语言是以右作为开始,所以paddingLeft明显不符合规则. 那我们使用paddingStart看一下.



    

<TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/tvOne"
      android:paddingStart="100dp"
      android:background="#ccefc8"
      android:text="@string/share"
      />

上述代码在阿拉伯语下的效果图:
[paddingStart Arabic]
高级处理

    android:layoutDirection 设置组件的布局方向
    android:textDirection 设置组件文字的方向
    android:textAlignment 设置组件文字的对齐
    getLayoutDirectionFromLocale() 获取指定Locale的方向
    创建单独的资源文件夹,以’ldrtl’(layout direction right-to-left)为后缀.如layout_ldrtl
    使用Hierarchy Viewer来查看View的start/end属性,layoutDirection,textDirection,textAlignment属性.

贴代码
manifest


    
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rtldemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

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

MainActivity.java


    

package com.example.rtldemo;

import java.util.ArrayList;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

  private static final String LOGTAG = "MainActivity";

  @SuppressLint("NewApi")
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main_layout);
      ListView listView = (ListView)findViewById(R.id.lvOne);
      
      int layoutDirection = TextUtils.getLayoutDirectionFromLocale(Locale.getDefault());
      Log.i(LOGTAG, "onCreate layoutDirection=" + layoutDirection);
      TestAdapter adapter = new TestAdapter();
      adapter.mData.add(getResources().getString(R.string.share));
      adapter.mData.add(getResources().getString(R.string.share));
      listView.setAdapter(adapter);
      
  }
 
  class TestAdapter extends BaseAdapter {
      ArrayList<String> mData = new ArrayList<String>();
      @Override
      public int getCount() {
          return mData.size();
      }

      @Override
      public String getItem(int position) {
          return mData.get(position);
      }

      @Override
      public long getItemId(int position) {
          return position;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View view = null;
          view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.listview_item, null);
          ImageView imgView = (ImageView)view.findViewById(R.id.itemImageView);
          imgView.setImageResource(R.drawable.ic_launcher);
          TextView textView = (TextView)view.findViewById(R.id.itemTextView);
          textView.setText(mData.get(position));
          return view;
      }
      
  }
}

main_layout.xml


    

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/share"
        />

      <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/tvOne"
      android:paddingStart="100dp"
      android:background="#ccefc8"
      android:text="@string/share"
      />
 
  <EditText
      android:id="@+id/etOne"
      android:layout_width="100dp"
      android:layout_height="wrap_content"
      />
 
  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      >
      <CheckBox
          android:layout_width="100dp"
          android:layout_height="100dp"
          />
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="100dp"
          android:text="@string/share"
          />
  </LinearLayout>
 
  <ListView
      android:id="@+id/lvOne"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      ></ListView>
 

</LinearLayout>

listview_item.xml


    

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
  <ImageView
      android:id="@+id/itemImageView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      />
 
  <TextView
      android:id="@+id/itemTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      />

</LinearLayout>



http://blog.csdn.net/u013276373/article/details/51766502

androidの设计的布局在阿拉伯语下界面错乱的解决方法
[摘要:androidの计划的结构正在阿推伯语下界里紊乱的办理方式 左侧是有题目图片,,,右侧是精确的图片 (1)正在AndroidManifest.xml声明文件的application元素中,增加” android:supportsRtl=true” (2)建]

androidの设计的布局在阿拉伯语下界面错乱的解决方法

(1)在AndroidManifest.xml声明文件的元素中,添加” android:supportsRtl=true”
(2)修改应用程序中layout的“left/right”布局属性,改为对应的”start/end”布局
paddingStart paddingEnd layout_marginStart layout_marginEnd layout_alignParentStart layout_alignParentEnd
替换
paddingLeft paddingRight layout_marginLeft layout_marginRight layout_alignParentLeft layout_alignParentRight
(3)混合字符或者纯英文下,TextView没有居右。比如列表中部分纯英文字串靠左显示,翻译过的靠右显示
找到其定义的layout文件
用 match_parent替换wrap_content,如果替换后还是没有效果新增
android:textDirection=”locale”这个属性。
代码控制
String language = Locale.getDefault().getLanguage();
if(language.equals(“ar”)”|language.equals(“fa”)){
TextView textview =(textview )view;
textview.setGravity(Gravity.RIGHT);
(4)对于一些图片方向不对的问题
对于这些图片需要按照重新设计,然后放到drawable-ldrtl-hdpi文件夹下
以上仅供参考,,


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值