在android中设置自定义警报对话框的背景

时间:2013-11-23 10:53:31

标签: android android-alertdialog

我想创建一个自定义AlertDialog,我将Layout设为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="@drawable/appbg">

<LinearLayout
        android:id="@+id/title_template"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:background="@drawable/alert_bg">

        <ImageView
            android:id="@+id/titleIcon"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="top"
            android:layout_marginLeft="6dp"
            android:paddingRight="10dip"
            android:paddingTop="6dip"
            android:src="@drawable/warning" 
            android:contentDescription="@string/image"/>

        <TextView
            android:id="@+id/alertTitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

    </LinearLayout>
    <ImageView android:id="@+id/titleDivider"
        android:layout_width="fill_parent"
        android:layout_height="5dip"
        android:scaleType="fitXY"
        android:gravity="fill_horizontal"
        android:src="@drawable/divider" 
        android:contentDescription="@string/image"/>
</LinearLayout>

自定义AlertDialog的类是:

public class Alert extends Builder {

protected final Context context;
protected TextView mTitle = null;
protected ImageView mIcon = null;
public Alert(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    this.context = context;

    View custonTitle = View.inflate(context, R.layout.alert_title, null);
    mTitle = (TextView) custonTitle.findViewById(R.id.alertTitle);
    mIcon = (ImageView) custonTitle.findViewById(R.id.titleIcon);
    setCustomTitle(custonTitle);

}

@Override
public Alert setIcon(Drawable icon) {
    // TODO Auto-generated method stub
    mIcon.setImageDrawable(icon);
    return this;
}

@Override
public Alert setIcon(int iconId) {
    // TODO Auto-generated method stub
    mIcon.setImageResource(iconId);
    return this;
}

@Override
public Alert setTitle(CharSequence title) {
    // TODO Auto-generated method stub
    mTitle.setText(title);
    return this;
}

@Override
public Alert setTitle(int titleId) {
    // TODO Auto-generated method stub
    mTitle.setText(titleId);
    return this;
}   

}

PROBLEM我无法获得正确的Dialog外观,它让我看起来像: THIS IS IMAGE

我希望它出现在Not Here的地方..默认消息背景不会出现...

1 个答案:

答案 0 :(得分:0)

为什么不按照简单的步骤创建自定义对话框?你可以到Android Developers。或者您可以按照这些教程

然后制作一个简单的布局并在此处发布完整的代码