【问题标题】:Using RTL direction in closeButton of android.support.v7.widget.SearchView在 android.support.v7.widget.SearchView 的 closeButton 中使用 RTL 方向
【发布时间】:2017-02-22 04:35:03
【问题描述】:

我正在尝试改变工具栏中 SearchView 的方向,这是我的尝试

layout.xml

android:layoutDirection="rtl"

menu.xml

<item android:id="@+id/action_search"
    android:title="@string/search_hint"
    android:icon="@mipmap/ic_search_icon"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView" />

Java 代码:

        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);

        getSupportActionBar().setCustomView(MenuItemCompat.getActionView(searchItem));

        mSearchView.setInputType(InputType.TYPE_CLASS_TEXT);
        mSearchView.setQueryHint(getString(R.string.search_hint));

        mSearchView.setGravity(Gravity.RIGHT);
        mSearchView.setTextDirection(View.TEXT_DIRECTION_RTL);    
        mSearchView.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);

        mSearchView.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        mSearchView.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT));

这就是结果,我成功地将 SearchView 添加到工具栏,现在是 RTL。但问题是“X”(closeButton)的位置不对,位置必须在左边。

【问题讨论】:

  • 您可以为工具栏使用自定义布局。 This may helpful to you
  • @KaushalideSilva 我的问题在 SearchView 中而不是在工具栏中

标签: android android-layout android-studio widget searchview


【解决方案1】:

问题是当你把layoutDirection改成rtl时,toolbar本身就会变成rtl。但layoutDirection 工具栏内的其他布局不会改变。和带有关闭按钮的 editText 处于嵌套布局中。

所以你必须找到父布局并为此改变方向。如果您在 SearchView 布局文件中看到。您会发现退出按钮位于带有search_plate id 的布局内

​​>

所以在onCreateOptionsMenu 设置SearchView 之后这样做

科特林

mSearchView.findViewById<View>(R.id.search_plate)?.let {
    it.layoutDirection = View.LAYOUT_DIRECTION_RTL
}

java

((View) mSearchView.findViewById(R.id.search_plate))
    .setLayoutDirection(View.LAYOUT_DIRECTION_RTL)

更新

或者您也可以将此 kotlin 扩展用于其他 rtl 问题。

fun ViewGroup.rtl() {
    val stack = Stack<View>()
    stack.add(this)
    while (stack.isNotEmpty()) {
        stack.pop().apply {
            layoutDirection = View.LAYOUT_DIRECTION_RTL
            textDirection = View.TEXT_DIRECTION_RTL
            (this as? ViewGroup)?.children?.forEach { stack.add(it) }
        }
    }
}

这样使用

view.rtl() //and view become rtl :)

【讨论】:

    【解决方案2】:

    您可以使用此方法在 RTL 语言环境中将 closeButton 向右移动:

      private static void handleCloseButtonRtlIssue(SearchView searchView) {
            ((LinearLayout) ((LinearLayout) searchView.getChildAt(0)).getChildAt(2)).getChildAt(1).setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
        }
    

    【讨论】:

      【解决方案3】:

      添加这个以改变 x 方向:

          View xIcon = ((ViewGroup) mSearchView.getChildAt(0)).getChildAt(2);
          xIcon.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
      

      【讨论】:

        【解决方案4】:

        在您的应用程序中支持Rtl。您必须在 manifest.xml 中设置 android:supportsRtl="true"

        记住最重要的一点

        将您应用的所有"Left/Right" 布局属性更改为Start/End

        在你的情况下,你正在设置。

        mSearchView.setGravity(Gravity.RIGHT);
        

        所以改成

        mSearchView.setGravity(Gravity.END);
        

        这里也改一下

        mSearchView.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT));
        

         mSearchView.setLayoutParams(new ActionBar.LayoutParams(Gravity.END));
        

        【讨论】:

        • 我已经将android:supportsRtl="true" 添加到我的manifest.xml 中,我只需将Gravity.RIGHT 替换为Gravity.END。但是 x 按钮位置也存在同样的问题
        猜你喜欢
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-29
        • 1970-01-01
        相关资源
        最近更新 更多