[英]AlertDialog being trimmed in Android Pie
我有一个应用程序,它从我的网络服务器的数据库中获取数据并将其显示到 AlertDialog 中,该应用程序在 Android 8 (Oreo) 及更低版本上运行良好,但在 Android 9 (Pie) 上,对话框正在被修剪,基本上隐藏了数据那是取来的。
我曾尝试使用 AlertDialogBuilder 但仍然是同样的问题,我是 Android 开发的新手,希望得到详细的解释。
public class BackgroundWorker extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx) {
context = ctx;
}
@Override
protected String doInBackground(String... strings) {
String destination = strings[0];
String query_url = "http://prateekrawat.000webhostapp.com/query.php";
try {
URL url = new URL(query_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("destination", "UTF-8") + "=" + URLEncoder.encode(destination, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) != null)
result = result + "\n" + line;
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Buses");
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="395dp"
android:layout_height="717dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginStart="8dp"
android:layout_marginTop="80dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:gravity="center"
android:text="Enter Destination"
app:layout_constraintBottom_toTopOf="@+id/editText3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.846" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:gravity="center"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="214dp" />
<Button
android:id="@+id/button4"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="177dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginTop="80dp"
android:onClick="Submit"
android:text="Submit"
android:textSize="20sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
演示图片
尝试创建自定义对话框样式并添加百分比和高度。
min_with_minor 是:
当对话框沿着短轴(即屏幕是纵向的)时,平台所需的对话框宽度的最小尺寸。 这可以是分数或维数。
Min_witdh_major 是:
当对话框沿着短轴(即屏幕是纵向的)时,平台所需的对话框宽度的最小尺寸。 这可以是分数或维数。
试试下面的例子。
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowMinWidthMajor">80%</item>
<item name="android:windowMinWidthMinor">80%</item>
<item name="windowFixedHeightMinor">80%</item>
<item name="windowFixedHeightMajor">80%</item>
<item name="android:windowBackground">@drawable/ubp_bg_white_rect</item>
<item name="colorAccent">#FFC107</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#FFFFFF</item>
<!-- Used for the background -->
<item name="android:background">#4CAF50</item>
</style>
AlertDialog.Builder myAlert = new AlertDialog.Builder(this, R.style.AlertDialogCustom);
myAlert.setTitle("Title");
myAlert.setMessage("1298201");
myAlert.show();
以下是奥利奥的示例结果:
以下是 Pie 的示例结果;
这是由于样式,请创建自定义样式并放在那里它会正常工作。
<style name="MyAlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
<item name="colorAccent">@color/colorAccent</item>
<item name="android:colorBackground">@color/alertDialogBackground</item>
<item name="android:windowBackground">@color/alertDialogBackground</item>
</style>
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity(), R.style.MyAlertDialogTheme);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.