簡體   English   中英

在Android中設置自定義警報對話框的背景

[英]setting background of custom alert dialog in android

我想創建一個自定義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的正確外觀,它使我看起來像: 這是圖像

我希望那應該出現在“ Not Here ..默認消息背景支架不會出現...

為什么不遵循簡單的步驟來創建“自定義對話框”? 您可以從Android Developers獲得。 或者您可以按照這些教程

然后進行簡單的布局並在此處發布完整的代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM