[英]Inflated AlertDialogs unreadable on Android 4.X while looking perfect on Android 2.X
為了顯示內容豐富,輸入要素對話,我使用Android的AlertDialog.Builder
創建的對象AlertDialog
,然后我使用系統的LayoutInflater
設置內容( setView(...)
一些XML布局文件。
這在Android 2.X(深色背景,白色文本)上看起來很完美:
但是在Android 4.X上根本無法讀取:
注意:當然,這些對話框並不相同,但是兩個對話框都會出現問題。
那我在做什么錯? 我使用Theme.Light
上<11和API級Theme.Holo.Light
上> = 11。
編輯1:這是我用來膨脹的代碼:
private LayoutInflater mGlobalInflater; // in Activity class
mGlobalInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE); // in Activity's onCreate()
mGlobalInflater.inflate(R.layout.input_dialog, null); // when needed
編輯2:這是“選擇日期”視圖的膨脹XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dp">
<DatePicker
android:id="@+id/input_date"
android:startYear="1900"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="@string/hint" />
</LinearLayout>
編輯3:正如我現在所發現的那樣,當使用mGlobalInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
時,在Android <= 2.3.3上可以正確呈現膨脹對話框內容mGlobalInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
並在使用mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
時在Android> = 3.0上正確呈現mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
。 因此, Context
會有所作為。 為什么?
編輯4:
該對話框是在Activity
的onContextItemSelected()
內部創建並膨脹的:
AlertDialog.Builder dialogPoup = new AlertDialog.Builder(MainActivity.this);
dialogPoup.setTitle("abc123");
final View inputDialog = mGlobalInflater.inflate(R.layout.input_dialog, null);
// setting some properties of the view and its child views
dialogPoup.setView(inputDialog);
這個對嗎? 還是我必須用一個變量mContext
(例如,將Activity
的上下文在onCreate()
分配給它)替換MainActivity.this
?
而不是像上面的代碼那樣使用getApplicationContext()
獲取LayoutInflater,而是使用Activity中的那個來代替:
mGlobalInflater = LayoutInflater.from(this); // in Activity's onCreate()
希望這將為對話框使用匹配的主題。
當您說您正在使用Theme.Light
和Theme.Holo.Light
,應用此主題的內容很重要。
它是您的Application
還是Activity
有所不同。 如果要在“ Application
級別上應用它,那么如果您從“應用程序”中獲得了“ Context
,則該主題會膨脹(類似於Activity
)。 如果應用程序和活動這兩個主題都不同,您會注意到差異。
當您通過上述@Joe的答案時,請記住這一點。 您提到@Joe的答案解決了api11 +的問題,但對於以前的問題卻沒有解決。
因此,只需看一下您的AndroidManifest,看看您對<application>
的主題有什么了解,以及您從中誇大Dialog的特定<activity>
主題。
希望能有所幫助
我發現的唯一可行的解決方案是:
LayoutInflater mInflater, mGlobalInflater;
mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= 11) {
mGlobalInflater = mInflater;
}
else {
mGlobalInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
}
現在可以使用mInflater
用於正常充氣(如:一個內部行ListView
)和mGlobalInflater
用於充脹的內部視圖AlertDialog
(使用setView()
然后,這將適用於所有API級別。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.