【问题标题】:Set LayoutDirection (RTL) for api lower than 17为低于 17 的 api 设置 LayoutDirection (RTL)
【发布时间】:2015-08-08 10:13:29
【问题描述】:

我要建立一个Persian app (RTL)。 我的应用包括 ListView 和 Navigation Drawer .

我在应用程序标签android:supportsRtl="true"中添加了Manifest

onCreate() 方法中:getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

我知道LayoutDirection 少于 17 的 api 不起作用。

对于 api 级别 17 及更高级别:

对于低于 17 的 api:

我该如何解决这个问题。 1-放置conditional statement 来检查android api并分配特定的布局? 2-在eclipse中使用文件夹layout-ldrtl? 3 - ...? 最好的方法是什么??

<android.support.v4.widget.DrawerLayout

       xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/drawer_layout"


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/myback">



<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="40dp"  >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp" >




        <EditText
        android:id="@+id/edtxtsearch"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
         android:hint="عبارت مورد جستجو"
          android:textSize="13dp"
         >

        <requestFocus />
    </EditText> 


        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_gravity="center"

            >


            <ListView
                        android:id="@+id/mylist"
                        android:layout_width="200dp"
                      android:layout_height="wrap_content"
                      android:layout_gravity="center_horizontal"
                      android:divider="@android:color/transparent"
                      android:dividerHeight="7dp"
                      android:scrollbarStyle="outsideInset"
                      />



           </LinearLayout>



</LinearLayout>




<FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

         >



    </FrameLayout>



     <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:background="#ffffff"
        android:dividerHeight="4dp"  
         android:divider="@android:color/transparent"     

       />

    </android.support.v4.widget.DrawerLayout>

【问题讨论】:

  • 不确定是否有很好的答案。对于布局,我倾向于使用 locale 或 ldrtl 文件夹。另见stackoverflow.com/questions/15746091/…
  • @QuantumTiger 几乎做到了。我只会补充一点,4.2 之前的一些供应商(设备制造商和移动运营商)有时会为 RTL 布局和双向文本提供他们的自定义解决方案。这有助于他们当时将产品推向 ME 市场,并使一些预安装的应用程序能够提供合理的用户体验。但从今天的角度来看,这增加了碎片化:准备好发现针对 AOSP 4.2 调整的应用在三星 Galaxy S2 (ME ROM) 或其他设备上会表现异常。
  • @P.Rai 我知道低于 4.2 的 Android 不支持 layoutDirection。我在问题的上下文中说过。我的意思是还有另一种设置右对齐导航抽屉的方法吗?

标签: android right-to-left


【解决方案1】:

使用来自 android.support.v4.view.ViewCompat 的 ViewCompat

像这样:

ViewCompat.setLayoutDirection (View yourView, int layoutDirection)

layoutDirection 是 ViewCompat 类的常量

【讨论】:

  • 谢谢,但此代码适用于 Android 4.2 及更高版本
  • 你确定吗?我已经使用了支持库!
  • LayoutDirection 未在 api 级别 16 及以下定义,因此我的应用程序中的 Drawelayout 也将位于屏幕左侧。
  • 它根本不起作用。检查来源 - 任何代码只有在 API >= 17 时才会执行。我真的很想知道这个答案怎么会有很多优点,因为主题中是关于低于 17 的 API 的信息。
  • 不适用于 17 以下的 API :|
【解决方案2】:

只需将要在 textView 中设置的每个字符串与 "\u202B" 连接起来

它只是强制文本为 RTL。

String textWhichIsLTR = "LTR or RTL text that you want to enforce to RTL";
String enforcedRTL = textWhichIsLTR + "\u202B";
fooTextView.setText(enforcedRTL);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 2012-12-05
    • 1970-01-01
    • 2021-01-19
    • 2021-05-13
    • 1970-01-01
    • 2013-12-24
    相关资源
    最近更新 更多