【问题标题】:Android setLayoutDirection on DialogFragmentDialogFragment上的Android setLayoutDirection
【发布时间】:2016-06-04 00:46:44
【问题描述】:

我最近完成了一个项目的构建,我的下一个目标是创建一个专门用于 RTL 的构建风格,一个重要的注意事项是我需要强制 RTL 布局,独立于设备语言。

所以我设法使用视图方法 setLayoutDirection 在大多数应用程序上强制 RTL 并旋转了所有不合作的视图,但由于某种原因,我无法镜像对话框该应用程序似乎仅在设备语言更改时才会翻转。 我试图在onCreateDialog中翻转对话框,访问getView,它返回null,我不确定我还能做什么......

有人可以帮我将 DialogFragment 翻转为 RTL 吗?

谢谢。

【问题讨论】:

    标签: java android dialog right-to-left dialogfragment


    【解决方案1】:

    AlartDialog 可以使用getWindow().getDecorView().setLayoutDirection(),布局方向会改变标题和内容,但不会改变按钮。

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    
        builder.setPositiveButton("OK", null);
        builder.setNegativeButton("Cancel", null);
    
        AlertDialog alertDialog = builder.create();
    
        // Here you can change the layout direction via setLayoutDirection()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            alertDialog.getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        }
    
        return alertDialog;
    }
    

    【讨论】:

    • 还有什么办法可以改变按钮的吗?
    猜你喜欢
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 2012-05-22
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多