简体   繁体   中英

setting background of custom alert dialog in android

I want to create a custom AlertDialog , I have the Layout as :

<?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>

Class for custom AlertDialog is :

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 is Im not being able to get the correct look of Dialog it gives me look like : 这是图像

I want that should appear in palce of Not Here .. the default message background shold not be appeared...

Why don't you follow simple steps to create a Custom Dialog? You can get at Android Developers . Or you can follow these tutorial

Then make a simple layout and post your complete code here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM