首页 > 解决方案 > 对话占据整个屏幕

问题描述

我正在创建一个带有数字选择器和按钮的对话,但不幸的是它占据了整个屏幕。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<NumberPicker
    android:id="@+id/numberPicker1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:descendantFocusability="blocksDescendants"

    android:layout_marginTop="64dp" />


<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/numberPicker1"
    android:text="Set" />

</RelativeLayout> 

这是我创建对话的方式

        final Dialog d = new Dialog(CreateShiftActivity.this, R.style.Theme_AppCompat);
    d.setTitle("NumberPicker");
    d.setContentView(R.layout.num_dialog);

标签: android

解决方案


尝试包装您的相对布局:

android:layout_width="wrap_content"
android:layout_height="wrap_content"

我建议以后使用约束布局。

希望能帮助到你。

编辑

试试这个约束:

<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <NumberPicker
            android:id="@+id/numberPicker1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="8dp"/>


    <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Set" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/numberPicker1"
            app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="8dp"
            android:layout_marginStart="8dp" android:layout_marginEnd="8dp"/>

</android.support.constraint.ConstraintLayout>

推荐阅读