Android 12设备上的Android对话裁剪问题
我们在市场上有许多应用程序使用对话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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为问题在于
onstart()
方法。您可以尝试像以下那样改变吗?
I think the problem is in
onStart()
method.Can you try to change like following?
这可能会有所帮助
this may help
三星的更新解决了这个问题!我不知道是什么基本问题是什么,但不再是一个问题。感谢所有对此思考的人。
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.