【问题标题】:Alert dialog Android 2.3 issue警报对话框 Android 2.3 问题
【发布时间】:2012-11-14 11:57:50
【问题描述】:

我遇到了警报对话框的问题,这是我的代码:

new AlertDialog.Builder(GestureShortcutsMainActivity.this)

.setTitle("Disclaimer")
.setIcon(R.drawable.alert_icon)
.setMessage(R.string.disclaimer)
.setPositiveButton("I Accept", 
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {


                            }
                        })
.setCancelable(false)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
})
.show();

这就是它在 android 4.0.4 中的样子:

这就是它在 android 2.3.1 中的样子:

我怎样才能让它在两个版本上都能正确显示?

【问题讨论】:

    标签: android android-alertdialog android-4.0-ice-cream-sandwich android-2.3-gingerbread


    【解决方案1】:

    您的主题在 Android 2.3 中不可用。 (例如:Android 2.3 不包括 Theme_Holo)

    public void setThemeForAPIVersion()
    {
        if((Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB))
        {
            setTheme(android.R.style.Theme_Holo);
        }
        else
        {
            setTheme(android.R.style.Theme_NoTitleBar);
        }
    }
    

    在你的活动调用的 onCreate 中

            setThemeForAPIVersion(); 
    

    【讨论】:

    • 其实这是我的问题。谢谢!
    【解决方案2】:

    我建议您将消息放在垂直滚动条内。 This question 显示了一个示例。让 XML 代码看起来像这样(这是不完整的......)

    <ScrollView android:id="scrollDialog">
        <LinearLayout android:orientation="vertical"
                android:scrollbars="vertical"
                android:scrollbarAlwaysDrawVerticalTrack="true">
            <TextView android:id="text" />
            <Button />
        </LinearLayout>
    </ScrollView>
    

    然后在您的代码中,找到 TextView,并将 AlertDialog 设置为使用该布局

    AlertDialog.Builder builder=new AlertDialog.Builder(this);
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout;
    layout = inflater.inflate(R.layout.scrollDialog,null);
    TextView text=(TextView) findViewById(R.id.text);
    text.setText(R.string.disclaimer);
    setContentView(layout);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      相关资源
      最近更新 更多