android:layout_alignParentBottom ="true"改变Dialog Activity的高度从wrap_content到match_parent

标签 android android-activity android-dialog android-relativelayout android-wrap-content

我在 Activity 中有一个 Button,它具有 Dialog 样式。 这是我的 Activity 的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Button" />

</RelativeLayout>

这是我的 Activity 的样式,它导致将其显示为 Dialog

<style name="Modal" parent="Theme.AppCompat.Light.Dialog">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowFullscreen">false</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

即使 android:windowFullscreen 为 false,此 Activity 仍显示为全屏!当我从 Activity xml 文件中删除 android:layout_alignParentBottom="true" 时,Activityheight改成wrap_content就没有问题了。我想知道如何在不删除 Button 属性的情况下解决这个问题。

这里可以看到显示Activity的结果

使用 android:layout_alignParentBottom="true"

enter image description here

并且没有 android:layout_alignParentBottom="true"

enter image description here

最佳答案

可以通过设置属性来实现

 android:layout_alignParentBottom="true"

以编程方式使用以下代码

    RelativeLayout relativeLayout = view.findViewById(R.id.relative_layout);
    Button button = relativeLayout.findViewById(R.id.button);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, view.getId());
    button.setLayoutParams(params);

在您的 xml 文件中,您需要设置一个 id 属性 并删除 android:layout_alignParentBottom="true"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/relative_layout"
android:layout_height="match_parent">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:text="Button" />

</RelativeLayout>

关于android:layout_alignParentBottom ="true"改变Dialog Activity的高度从wrap_content到match_parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38025579/

相关文章:

android - 打开权限管理器设置页面

TextView中url的Android Activity 链接

java - Activity 权限拒绝

安卓错误 : Unable to add window -- token null is not for an application

android - 在单击的项目正上方打开对话框,如上下文菜单

java - 带有游戏分数的自定义对话框 android

android - 以编程方式单击上下文操作栏上的“完成”按钮

java - 从 fragment 访问主要 Activity View

java - 找不到符号方法 findPreference(String)

android - 检测到屏幕覆盖 | (Android 6.x) 联想、红米和三星设备