2016-04-01 21 views
1

我目前使用AlertDialog來提示用戶並要求評分。它的工作原理與使用AS仿真器(Nexus 5 API 23)相同。AlertDialog按鈕文本格式在某些設備上完全錯誤

從模擬器:

enter image description here

但是,我測試的對話框了我朋友的設備上,LG的柱花草(安卓5.1.1,空氣污染指數22)和格式是完全錯誤的。按鈕順序錯誤,根本沒有正確格式化。

從LG柱花草:

enter image description here

我不知道如何去確保該格式是一個AlertDialog正確的,我真的不知道爲什麼格式不上LG正確Stylo,不幸的是我目前沒有其他設備來測試它。

在此先感謝!

AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 
     builder.setMessage(R.string.rateMeText) 
       .setPositiveButton(R.string.rateMe, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Intent intent = new Intent(Intent.ACTION_VIEW); 
         editor.putBoolean(KEY_REMIND_ME_LATER, false); 
         editor.commit(); 
         try { 
          intent.setData(Uri.parse(myContext.getString(R.string.playStoreMarketLink) + APP_PNAME)); 
          myContext.startActivity(intent); 
         }catch (Exception e){ 
          intent.setData(Uri.parse(myContext.getString(R.string.playStoreHttpLink) + APP_PNAME)); 
          myContext.startActivity(intent); 

         } 
         dialog.dismiss(); 
        } 
       }) 
       .setNegativeButton(R.string.noThanksAndDoNotAskAgain, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         if (editor != null) { 
          editor.putBoolean(KEY_REMIND_ME_LATER, false); 
          editor.commit(); 
          /*editor.putBoolean(KEY_DONT_SHOW_AGAIN, true);*/ 
          editor.commit(); 
         } 
         dialog.dismiss(); 
        } 
       }) 
       .setNeutralButton(R.string.remindMeLater, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         editor.putLong(KEY_LAUNCH_COUNT_PRESSED_REMIND, launches); 
         editor.putBoolean(KEY_REMIND_ME_LATER, true); 
         editor.commit(); 
         System.out.print(sharedPrefs.getLong(KEY_LAUNCH_COUNT_PRESSED_REMIND, 27727)); 
         dialog.dismiss(); 
        } 
       }); 
     builder.create().show(); 
+0

您是否使用API​​ 22(即5.1.1)在仿真器上測試了它? –

+0

您是使用'android.app.AlertDialog'還是'android.support.v7.app.AlertDialog'? –

+0

如果不設置樣式,即使設置應用程序主題,設備有時也會使用其自己的默認樣式。如果你不設置風格,它就是一種垃圾拍攝。 –

回答

3

您必須使用現有的樣式作爲父類創建樣式,然後在AlertDialog.Builder()方法中使用它。

它應該是這樣的:

<style name="myAlertStyle" parent="AlertDialog.Material"> 
    <item name="fullDark">@empty</item> 
    <item name="topDark">@empty</item> 
    <item name="centerDark">@empty</item> 
    <item name="bottomDark">@empty</item> 
    <item name="fullBright">@empty</item> 
    <item name="topBright">@empty</item> 
    <item name="centerBright">@empty</item> 
    <item name="bottomBright">@empty</item> 
    <item name="bottomMedium">@empty</item> 
    <item name="centerMedium">@empty</item> 
    <item name="layout">@layout/alert_dialog_material</item> 
    <item name="listLayout">@layout/select_dialog_material</item> 
    <item name="progressLayout">@layout/progress_dialog_material</item> 
    <item name="horizontalProgressLayout">@layout/alert_dialog_progress_material</item> 
    <item name="listItemLayout">@layout/select_dialog_item_material</item> 
    <item name="multiChoiceItemLayout">@layout/select_dialog_multichoice_material</item> 
    <item name="singleChoiceItemLayout">@layout/select_dialog_singlechoice_material</item> 
</style> 

,然後你只想用AlertDialog.Builder()這樣的:

AlertDialog.Builder(mContext, R.style.myAlertStyle); 
+0

順便說一下,您可以隨時查看您計算機上的sdk並查看所有默認樣式是如何創建的。那就是我從這裏得到這個樣本:)他們住在'pathToYourSdk/sdk/platforms/android - ##/data/res/values /' –

1

值得一提的是這是在相當多的錯誤api-22支持庫,因爲它試圖將所有按鈕放在一行上。它已經被固定在api-23支持庫中,通過切換到每個按鈕在自己的行上的新佈局。

我在兩個不同的項目中使用支持庫AlertDialog以及包含KitKat 4.4.4的設備測試了您的代碼。

用於這兩個測試的相同的代碼:

import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar); 
     setSupportActionBar(toolbar); 

     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("If you like this app, please rate it on the Google Play Store!") 
       .setPositiveButton("Rate me!", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 

        } 
       }) 
       .setNegativeButton("No thanks and do not ask me again", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 

        } 
       }) 
       .setNeutralButton("Remind me later", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 

        } 
       }); 
     builder.create().show(); 
    } 
} 

首先,在項目與此gradle這個配置(所有API-22設置):

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 22 
    buildToolsVersion "21.1.2" 

    defaultConfig { 
     applicationId "com.example.myapplication" 
     minSdkVersion 15 
     targetSdkVersion 22 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:22.2.1' 
} 

這是結果:

enter image description here

然後,在這個配置不同的項目中離子(所有API-23設置):

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "21.1.2" 

    defaultConfig { 
     applicationId "com.example.testprojecttwo" 
     minSdkVersion 15 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:23.1.1' 
    compile 'com.android.support:design:23.1.1' 
} 

這是相同的代碼的結果:

enter image description here

所以,一個固定的,這將是使用API​​-23支持庫。

相關問題