android - DialogFragment 太宽,无法居中
问题描述
我已经尝试谷歌很长一段时间了,但似乎没有一个答案对我有用。我在新片段中有 AlertDialog,无论我做什么,它都不会居中。我觉得我缺少一些基本的东西,比如以错误的方式夸大视图。
这是它现在的样子: AlertDialog not centered
我希望视图居中而不是左侧。只要按钮居中,去除多余的空白也很好,但不是必需的。
这是我的代码的样子:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
>
<!--Row 1. (colors 1-4)-->
<LinearLayout...>
<!--Row 2. (colors 5-8)-->
<LinearLayout...>
<!--Row 3. (colors 9-12)-->
<LinearLayout...>
</LinearLayout>
和
public class dialog_ThemePicker extends DialogFragment implements View.OnClickListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_themepicker, null);
...
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
alertDialog.setTitle(R.string.dialog_theme_title);
alertDialog.setView(view);
alertDialog.show();
return alertDialog;
}
我试图将整个 XML 包装到RelativeLayout
LayoutParams 和 StackOverflow 中的多个其他解决方案中。我错过了什么?
解决方案
尝试将 match_parent 赋予您的 rootLayout 宽度,然后赋予center
重力选项这是应该的。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical">
<!--Row 1. (colors 1-4)-->
<LinearLayout...>
<!--Row 2. (colors 5-8)-->
<LinearLayout...>
<!--Row 3. (colors 9-12)-->
<LinearLayout...>
</LinearLayout>
推荐阅读
- javascript - 如何从浏览器中检索旧的控制台日志?
- python - JIRA python库JiraError 400:无法设置字段“摘要”
- docker - 从另一台主机连接 Docker 内的 Kafka
- css - 像素显示不均
- c# - 部署 VS 2019 500 内部错误时 Web Api 不起作用
- graphql - graphQL: SyntaxError: Unexpected token " 在 JSON 中的位置 0
- python - Python 中更优雅的 arr
- ionic-framework - 如何仅在 Ion 5 网格中的 lg 设备中添加填充
- gitlab - 从多项目管道运行手动作业
- laravel - 在 laravel 8 中创建购物车的最佳方法是什么?