如何在Android中设置DialogFragment的大小

问题描述 投票:0回答:1

我有一个 DialogFragment,我想将其大小设置为整个屏幕的 80%。为此,我使用以下代码:

public class DialogFR_LevelEnd_Test extends DialogFragment {


    public DialoagFragmentLevelEndingBinding binding;


    public static DialogFR_LevelEnd_Test newInstance(double co2SavingsScore, double neededCO2SavingScore, int currentLevel) {
        DialogFR_LevelEnd_Test fragment = new DialogFR_LevelEnd_Test();
        Bundle args = new Bundle();
        fragment.setArguments(args);

        return fragment;
    }

    public void onViewCreated(Bundle savedInstanceState) {

        //Attempt 1
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        int screenWidth = (int) (displayMetrics.widthPixels * 0.80);
        int screenHeight = (int) (displayMetrics.heightPixels * 0.80);

        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
        params.width = screenWidth;
        params.height = screenHeight;
        getDialog().getWindow().setAttributes(params);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DialoagFragmentLevelEndingBinding.inflate(inflater, container, false);

        //Attempt 2
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        int screenWidth = (int) (displayMetrics.widthPixels * 0.80);
        int screenHeight = (int) (displayMetrics.heightPixels * 0.80);

        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
        params.width = screenWidth;
        params.height = screenHeight;
        getDialog().getWindow().setAttributes(params);


        return binding.getRoot();

    }



    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }


}

所以我尝试了两次,但都不起作用。无论我选择什么号码,尺寸始终相同。

这是 DialogFragment 的 XML 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >



    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/right_outer_constraintLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.44"
        app:layout_constraintHeight_percent="0.65"
        android:orientation="vertical"
        android:background="@drawable/rim"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.98"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:padding="10dp"
        app:layout_constraintVertical_bias="0.9">


        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/rigthInnerConstraintLayout"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/white"
            app:layout_constraintBottom_toBottomOf="@+id/right_outer_constraintLayout"
            app:layout_constraintEnd_toEndOf="@+id/right_outer_constraintLayout"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="@+id/right_outer_constraintLayout"

            app:layout_constraintTop_toTopOf="@+id/right_outer_constraintLayout"
            app:layout_constraintVertical_bias="1.0">


            <LinearLayout
                android:id ="@+id/linearLayoutForButtons"
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:orientation="horizontal"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                >


                <ToggleButton
                    android:id="@+id/tbutton_highScore_lastWeek"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/tbutton_selector"
                    android:textAllCaps="false"
                    android:textOff="@string/last_week"
                    android:textOn="@string/last_week"
                    android:textSize="10sp"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:ignore="TouchTargetSizeCheck" />

                <View
                    android:id="@+id/line1"
                    android:layout_width="3dp"
                    android:layout_height="match_parent"
                    android:background="#bcbcbc" />

                <ToggleButton
                    android:id="@+id/tbutton_highScore_lastMonth"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/tbutton_selector"
                    android:textAllCaps="false"
                    android:textOff="@string/last_month"
                    android:textOn="@string/last_month"
                    android:textSize="10sp"
                    tools:ignore="TouchTargetSizeCheck" />

                <View
                    android:id="@+id/line2"
                    android:layout_width="3dp"
                    android:layout_height="match_parent"

                    android:background="#bcbcbc" />

                <ToggleButton
                    android:id="@+id/tbutton_highScore_overall"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/tbutton_selector"
                    android:textAllCaps="false"
                    android:textOff="@string/overall"
                    android:textOn="@string/overall"
                    android:textSize="10sp"
                    tools:ignore="TouchTargetSizeCheck" />
            </LinearLayout>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_highScoreStatisticsToBeDisplayed"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginLeft="4dp"
                android:layout_marginTop="4dp"
                android:layout_marginRight="4dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/linearLayoutForButtons" />




        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>





    <TextView
        android:id="@+id/textView_LevelFinished"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Level Finished"
        android:textSize="45sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.50"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.05" />

    <ImageView
        android:id="@+id/imageView_CloseSymbol"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@+id/textView_LevelFinished"


        app:layout_constraintStart_toEndOf="@+id/textView_LevelFinished"
        app:layout_constraintTop_toTopOf="@+id/textView_LevelFinished"
        app:layout_constraintVertical_bias="0.512"
        app:srcCompat="@drawable/ic_close_symbol_foreground" />

    <ImageView
        android:id="@+id/imageView_Checkmark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@+id/textView_LevelFinished"

        app:layout_constraintStart_toEndOf="@+id/textView_LevelFinished"
        app:layout_constraintTop_toTopOf="@+id/textView_LevelFinished"
        app:srcCompat="@drawable/ic_check_mark_foreground" />

    <TextView
        android:id="@+id/textView_LevelFinishedMessageCO2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:text="You needed to save x g of CO2 and you got y g"
        android:textSize="14sp"
        app:layout_constraintHorizontal_bias="0.02"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView_CloseSymbol" />

    <TextView
        android:id="@+id/textView_LevelFinishedMessageGas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:text="You saved 10 kWh of Gas"
        android:textSize="14sp"
        app:layout_constraintEnd_toEndOf="@+id/textView_LevelFinishedMessageCO2"
        app:layout_constraintHorizontal_bias="0.4"
        app:layout_constraintStart_toStartOf="@+id/textView_LevelFinishedMessageCO2"
        app:layout_constraintTop_toBottomOf="@+id/textView_LevelFinishedMessageCO2" />

    <ImageView
        android:id="@+id/imageViewGasSymbol"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        app:layout_constraintBottom_toBottomOf="@+id/textView_LevelFinishedMessageGas"
        app:layout_constraintStart_toEndOf="@+id/textView_LevelFinishedMessageGas"
        app:layout_constraintTop_toTopOf="@+id/textView_LevelFinishedMessageGas"
        app:srcCompat="@drawable/gas2" />

    <ImageView
        android:id="@+id/imageView_NextLevelSymbol"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/textView_RepeatLevel"
        app:layout_constraintEnd_toEndOf="@+id/textView_LevelFinishedMessageCO2"
        app:layout_constraintHorizontal_bias="0.85"
        app:layout_constraintStart_toStartOf="@+id/textView_LevelFinishedMessageCO2"
        app:layout_constraintTop_toTopOf="@+id/imageView_RepeatSymbol"
        app:srcCompat="@drawable/ic_arrow_forward_foreground" />

    <ImageView
        android:id="@+id/imageView_RepeatSymbol"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="@+id/textView_LevelFinishedMessageCO2"
        app:layout_constraintHorizontal_bias="0.35"
        app:layout_constraintStart_toStartOf="@+id/textView_LevelFinishedMessageCO2"
        app:layout_constraintTop_toBottomOf="@+id/textView_HighscoreMessagePositions"
        app:srcCompat="@drawable/ic_repeat_symbol_foreground" />

    <TextView
        android:id="@+id/textView_RepeatLevel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/repeat_level"
        android:textStyle="bold"
        android:textSize="15sp"
        app:layout_constraintEnd_toEndOf="@+id/imageView_RepeatSymbol"
        app:layout_constraintStart_toStartOf="@+id/imageView_RepeatSymbol"
        app:layout_constraintTop_toBottomOf="@+id/imageView_RepeatSymbol" />

    <TextView
        android:id="@+id/textView_NextLevel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next_level"
        android:textStyle="bold"
        android:textSize="15sp"
        app:layout_constraintEnd_toEndOf="@+id/imageView_NextLevelSymbol"
        app:layout_constraintStart_toStartOf="@+id/imageView_NextLevelSymbol"
        app:layout_constraintTop_toBottomOf="@+id/imageView_NextLevelSymbol" />

    <ImageView
        android:id="@+id/imageView_leaf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        app:layout_constraintBottom_toBottomOf="@+id/textView_LevelFinishedMessageCO2"
        app:layout_constraintStart_toEndOf="@+id/textView_LevelFinishedMessageCO2"
        app:layout_constraintTop_toTopOf="@+id/textView_LevelFinishedMessageCO2"
        app:srcCompat="@drawable/leaf" />

    <TextView
        android:id="@+id/textView_TopScores"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Top Scores for this Level"
        android:textSize="21sp"
        android:layout_marginBottom="4dp"
        app:layout_constraintBottom_toTopOf="@+id/right_outer_constraintLayout"
        app:layout_constraintEnd_toEndOf="@+id/right_outer_constraintLayout"
        app:layout_constraintStart_toStartOf="@+id/right_outer_constraintLayout" />

    <TextView
        android:id="@+id/textView_HighscoreMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:text="You made it to the Top Score list"
        android:textSize="14sp"
        app:layout_constraintEnd_toEndOf="@+id/textView_LevelFinishedMessageCO2"
        app:layout_constraintStart_toStartOf="@+id/textView_LevelFinishedMessageCO2"
        app:layout_constraintTop_toBottomOf="@+id/textView_LevelFinishedMessageGas" />

    <TextView
        android:id="@+id/textView_HighscoreMessagePositions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1st this week, 3rd this month, 6th overall"
        android:textSize="14sp"
        android:layout_marginTop="20dp"
        app:layout_constraintEnd_toEndOf="@+id/textView_HighscoreMessage"
        app:layout_constraintStart_toStartOf="@+id/textView_HighscoreMessage"
        app:layout_constraintTop_toBottomOf="@+id/textView_HighscoreMessage" />
</androidx.constraintlayout.widget.ConstraintLayout>

你知道该怎么做吗?在 XML 布局文件中还是在 Java 文件中?

java android android-dialogfragment
1个回答
0
投票

您可以将自定义

alertDialogTheme
添加到应用程序的主题中,以覆盖
windowFixedWidthMajor
windowFixedWidthMinor
属性。

示例:

    <!--Assuming your app is using material3 theme. -->
    <style name="CustomAlertDialogStyle" parent="ThemeOverlay.Material3.Dialog.Alert">
        <item name="windowFixedWidthMajor">80%</item><!--for landscape-->
        <item name="windowFixedWidthMinor">80%</item><!--for portrait-->
        <!--You can also create different dimen values for different screen sizes and assign the dimen to above-->
    </style>

然后在您的应用程序主题中:

<item name="alertDialogTheme">@style/CustomAlertDialogTheme</item>

注意:这样做将适用于全球。

© www.soinside.com 2019 - 2024. All rights reserved.