【问题标题】:Customize alertdialog android api 10自定义 alertdialog android api 10
【发布时间】:2014-02-07 09:24:18
【问题描述】:

我想知道如何使用sdk api 10 自定义AlertDialog 的主题/样式。我知道如何从 11 开始,但不知道如何从 10 开始。

XML 布局文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    style="@style/dialog_theme" >
    <LinearLayout android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="vertical">
        <TextView 
            android:id="@+id/disclaimerText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:text="@string/disclaimer"
            android:padding="5dp"/>

        <CheckBox
            android:id="@+id/checkDisclaimer"
            style="?android:attr/textAppearanceMedium"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="@string/agree" />
    </LinearLayout>
</ScrollView>

JAVA 资源文件

AlertDialog.Builder builder = new AlertDialog.Builder(this);
//Setting Dialog Title
builder.setTitle("Disclaimer");
//Setting Dialog Message
builder.setMessage(R.string.disclaimer);
View view = (View) LayoutInflater.from(this).inflate(R.layout.layout_disclaimer, null);
builder.setView(view);

【问题讨论】:

    标签: java android styles android-alertdialog


    【解决方案1】:

    你可以试试这个:

    View myView = View.inflate(this, R.layout.customize_dialog, null);
    
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
    builder.setView(myView);
    builder.setTitle("Customize dialog");
    builder.setCancelable(false);
    AlertDialog alert=builder.create();
    alert.show();
    

    您必须在 res/layout/ 中创建一个名为 customize_dialog.xml 的布局, 您还可以添加到“myView”其他视图对象。

    添加典型的警报按钮:

    builder.setPositiveButton("Ok",new OnClickListener(){
    public void onClick(DialogInterface dialog, int id){
            //action to do
            dialog.dismiss();
        }
    });
    
    
    builder.setNegativeButton("Close",new OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
            //action to do
            dialog.dismiss();
        }
    });
    

    希望对你有帮助。

    【讨论】:

    • 我已经创建了一个自定义布局并添加到对话框中,但问题仍然不同,在我的布局中我添加了 style = "@ style / dialog_theme" 但没有,我仍然看到布局系统的。我已经编辑了我的答案
    猜你喜欢
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多