[英]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.