【问题标题】:Android - RTL layout becomes LTR after inflationAndroid - 充气后RTL布局变为LTR
【发布时间】:2019-05-24 07:10:21
【问题描述】:

我的Android 项目中有一个自定义的Action Bar XML 布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layoutDirection="rtl"
    android:orientation="horizontal"
    android:textDirection="rtl">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Some Text"
        android:textColor="@color/white" />

</LinearLayout>

现在如果尝试像这样加载和显示这个Action Bar

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.id.custom_action_bar);

布局加载为RTL,文本方向为RTL,一切正常,但我想让标题动态化,所以我决定将inflate布局文件转换为View,然后更改TextView 的文字是这样的:

View customActionBar = LayoutInflater.from(this)
            .inflate(R.layout.custom_action_bar, null);
TextView actionBarTitle = customActionBar.findViewById(R.id.tv_name);
actionBarTitle.setText("Any Text");
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(customActionBar);

然后布局和文本方向变成LTR。我尝试以编程方式将方向设置为RTL,但也没有用。

这里有什么问题?我应该怎么做才能解决它?

【问题讨论】:

    标签: android layout view android-actionbar right-to-left


    【解决方案1】:

    您需要在 AndroidManifest.xml 中添加 android:supportRtl="true" 以具有 rtl 布局

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      相关资源
      最近更新 更多