我在 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"
时,Activity
的 height
改成wrap_content
就没有问题了。我想知道如何在不删除 Button
属性的情况下解决这个问题。
这里可以看到显示Activity的结果
使用 android:layout_alignParentBottom="true"
并且没有 android:layout_alignParentBottom="true"
。
最佳答案
可以通过设置属性来实现
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/