Android 12设备上的Android对话裁剪问题

发布于 2025-02-13 13:04:01 字数 6459 浏览 0 评论 0原文

我们在市场上有许多应用程序使用对话fragment(AppCompatdialogfragment版本)。我们发现,在某些设备,三星选项卡S7上,对话框被截断,以免显示底部的信息和动作按钮。我尝试过的任何事情都不会以在所有设备上保持一致的方式解决此问题。我制作了一个简单的布局和代码以演示。该代码按照截断的图像插入到实时应用程序中时,在新创建的测试应用中,它正常在同一设备上显示!所有主题,清单设置看起来都相似。我已经花了很多天的时间了,因此任何建议都将不胜感激。

package com.example.dialogclipping;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.fragment.app.DialogFragment;

public class DlgTest extends AppCompatDialogFragment {



    static DlgTest newInstance() {

        return new DlgTest();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogStyle);

    }

    @Override
    public void onStart() {
        super.onStart();
        int targetWidth = 1000;
        int targetHeight = 0;

        // check if specific size setting is required
        if (targetWidth != 0 || targetHeight != 0) {

            var dialog = getDialog();
            if (dialog == null) return;
            var window = dialog.getWindow();
            if (window == null) return;

            var lp = window.getAttributes();
            // overwrite dimensions as appropriate
            if (targetWidth > 0) lp.width = targetWidth;
            if (targetHeight > 0) lp.height = targetHeight;
            window.setAttributes(lp);
        }

    }



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dlg_test_layout, container, false);
        return view;
    }
    int index = 2;

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

        view.setBackgroundColor(0xFF92580C);
        final var closeButton = view.findViewById(R.id.yesButton);
        closeButton.setOnClickListener(v -> {
            /*
            var message = "Height is " + view.getHeight() + "; bottom view at " + bottomField.getBottom();
            legend.setText(message);
            var afterField = view.findViewById(fields[index]);
            afterField.setVisibility(View.VISIBLE);
            index = (index + 1) % fields.length;

             */
            //dismiss();
        });
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/SettingsDialog"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="8dp"
    android:paddingBottom="8dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/header"
        style="@style/DialogText.Heading"
        android:minEms="16"
        android:layout_gravity="left"
        android:text="Header line"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:background="@color/blue"/>

    <View
        android:id="@+id/middleView"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:layout_marginTop="10dp"
        android:background="@color/black">

    </View>
    <LinearLayout
        android:id="@+id/ButtonBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="right">

        <Button
            android:id="@+id/yesButton"
            style="@style/DialogButton.Large"
            android:textSize="20dp"
            android:layout_width="0dip"
            android:layout_weight="2"
            android:text="Close"
            android:visibility="visible"/>

    </LinearLayout>

    <View
        android:id="@+id/bottomView"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:background="@color/blue">

    </View>
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.dialogclipping">

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="false"
        android:xlargeScreens="true"/>


    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:resizeableActivity="false"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppBaseTheme"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="sensorLandscape"
            android:configChanges="orientation|keyboardHidden|screenSize|smallestScreenSize"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.NoActionBar">
    </style>


    <style name="DialogStyle" parent="Theme.AppCompat.Dialog">
    </style>

We have a number of Apps on the market that use DialogFragment (the AppCompatDialogFragment version). We are finding that on certain devices, Samsung TAB S7 for one, dialogs are truncated so that information and action buttons at the bottom are not displayed. Nothing I have tried will resolve this issue in a way that is consistent across all devices. I have produces a simple layout and code to demonstrate. This code when inserted in to the live app displays as per the truncated image, in a newly created test app it displays normally on the same device! All theming, manifest settings appear similar. I have spent many days on this so any suggestions would be much appreciated.

package com.example.dialogclipping;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.fragment.app.DialogFragment;

public class DlgTest extends AppCompatDialogFragment {



    static DlgTest newInstance() {

        return new DlgTest();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogStyle);

    }

    @Override
    public void onStart() {
        super.onStart();
        int targetWidth = 1000;
        int targetHeight = 0;

        // check if specific size setting is required
        if (targetWidth != 0 || targetHeight != 0) {

            var dialog = getDialog();
            if (dialog == null) return;
            var window = dialog.getWindow();
            if (window == null) return;

            var lp = window.getAttributes();
            // overwrite dimensions as appropriate
            if (targetWidth > 0) lp.width = targetWidth;
            if (targetHeight > 0) lp.height = targetHeight;
            window.setAttributes(lp);
        }

    }



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dlg_test_layout, container, false);
        return view;
    }
    int index = 2;

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

        view.setBackgroundColor(0xFF92580C);
        final var closeButton = view.findViewById(R.id.yesButton);
        closeButton.setOnClickListener(v -> {
            /*
            var message = "Height is " + view.getHeight() + "; bottom view at " + bottomField.getBottom();
            legend.setText(message);
            var afterField = view.findViewById(fields[index]);
            afterField.setVisibility(View.VISIBLE);
            index = (index + 1) % fields.length;

             */
            //dismiss();
        });
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/SettingsDialog"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="8dp"
    android:paddingBottom="8dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/header"
        style="@style/DialogText.Heading"
        android:minEms="16"
        android:layout_gravity="left"
        android:text="Header line"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:background="@color/blue"/>

    <View
        android:id="@+id/middleView"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:layout_marginTop="10dp"
        android:background="@color/black">

    </View>
    <LinearLayout
        android:id="@+id/ButtonBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="right">

        <Button
            android:id="@+id/yesButton"
            style="@style/DialogButton.Large"
            android:textSize="20dp"
            android:layout_width="0dip"
            android:layout_weight="2"
            android:text="Close"
            android:visibility="visible"/>

    </LinearLayout>

    <View
        android:id="@+id/bottomView"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:background="@color/blue">

    </View>
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.dialogclipping">

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="false"
        android:xlargeScreens="true"/>


    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:resizeableActivity="false"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppBaseTheme"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="sensorLandscape"
            android:configChanges="orientation|keyboardHidden|screenSize|smallestScreenSize"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.NoActionBar">
    </style>


    <style name="DialogStyle" parent="Theme.AppCompat.Dialog">
    </style>

Real app image

Test app image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

国际总奸 2025-02-20 13:04:01

我认为问题在于onstart()方法。
您可以尝试像以下那样改变吗?

    @Override
    public void onStart() {
        super.onStart();
        Window win = getDialog().getWindow();
        if (win == null)
            return;

        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams params = win.getAttributes();
        int contextRect = getContextRect((Activity) mContext);

        params.gravity = Gravity.CENTER; //you can change Gravity in here
        params.width = ViewGroup.LayoutParams.MATCH_PARENT; //you can change width in here
        int screenHeight = contextRect == 0 ? ViewGroup.LayoutParams.MATCH_PARENT : contextRect;
        params.height = screenHeight;
        win.setAttributes(params);
    }

    private int getContextRect(Activity activity) {
        Rect outRect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
        return outRect.height();
    }

I think the problem is in onStart() method.
Can you try to change like following?

    @Override
    public void onStart() {
        super.onStart();
        Window win = getDialog().getWindow();
        if (win == null)
            return;

        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams params = win.getAttributes();
        int contextRect = getContextRect((Activity) mContext);

        params.gravity = Gravity.CENTER; //you can change Gravity in here
        params.width = ViewGroup.LayoutParams.MATCH_PARENT; //you can change width in here
        int screenHeight = contextRect == 0 ? ViewGroup.LayoutParams.MATCH_PARENT : contextRect;
        params.height = screenHeight;
        win.setAttributes(params);
    }

    private int getContextRect(Activity activity) {
        Rect outRect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
        return outRect.height();
    }
旧瑾黎汐 2025-02-20 13:04:01

这可能会有所帮助

   override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setStyle(STYLE_NORMAL, R.style.FullscreenDialogStyle)
    dialog?.let {
        it.window?.setLayout(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT
        )
    }
}

    <style name="FullscreenDialogStyle">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>

this may help

   override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setStyle(STYLE_NORMAL, R.style.FullscreenDialogStyle)
    dialog?.let {
        it.window?.setLayout(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT
        )
    }
}

    <style name="FullscreenDialogStyle">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
旧情勿念 2025-02-20 13:04:01

三星的更新解决了这个问题!我不知道是什么基本问题是什么,但不再是一个问题。感谢所有对此思考的人。

This problem has been resolved by an update by Samsung! I have no idea what the underlying problem was but it no longer is an issue. Thanks to all those who put some thought into it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文