[英]Android Custom Alert Dialog Design
我正在嘗試實現一個基本的自定義警報對話框。
它應該看起來像這樣
使用以下XML代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/tv_custom_dialog_event"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@drawable/actionbar_background"
android:gravity="top"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="100dp"
android:text="Delete Event ?"
android:textColor="@color/white"
android:textSize="20sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#DFDFDF" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="@+id/btn_custom_dialog_events_cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_action_cancel" />
<ImageButton
android:id="@+id/btn_custom_dialog_events_true"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_action_accept" />
</LinearLayout>
</LinearLayout>
但是,當我在模擬器上運行它時,它看起來像這樣:
有人知道為什么會發生這種情況以及我該如何解決嗎?
假設您有一個擴展Dialog的類,則可以執行以下操作:
首先在styles.xml中定義一個樣式,如下所示:
<style name="CustomDialogThemeTrasparent" parent="@android:style/Theme.Dialog">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
然后在自定義對話框類的構造函數中設置以下主題:
public class MyCustomDialog extends Dialog {
public MyCustomDialog(final Context context)
{
super(context, R.style.CustomDialogThemeTrasparent);
}
要將自定義布局設置為視圖,您可以創建一個名為setTheme()的函數,然后在對話框的show()中調用該函數,即使具有參數和完善的布局,您也可以創建一個更通用的類,用於顯示您的自定義對話框遍布整個應用,如下所示:
//Function to set the layout when the dialog is instantiated, here we
//set the layout and if you want you can set parameter to
//show/hide controls/views and can show different types of dialogs with the same class but with a unified style
private void setTheme()
{
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = layoutInflater.inflate(R.layout.custom_dialog, null);
this.setContentView(mView);
}
然后您可以使函數顯示對話框,例如:
public void showDeletionDialog(String pMessage)
{
this.setTheme();
this.show();
}
希望對你有幫助
試試這種方法。 您可以增加布局:
public AlertDialog displayLayoutDialog(int layout,final Context context, int theme){
AlertDialog.Builder builder = new AlertDialog.Builder(context, theme);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(layout, null);
builder.setView(view);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog= builder.create();
dialog.show();
Button tb = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
tb.setOnClickListener(new CustomListener(dialog, context));
return dialog;
}
而您的偵聽器將進行所有驗證和您需要執行的其他任何操作:
public class CustomListener implements View.OnClickListener {
private final Dialog dialog;
private Context context;
public CustomListener(Dialog dialog, Context context) {
this.dialog = dialog;
this.context = context;
}
@Override
public void onClick(View v) {
Toast.makeText(context, "Custom Layout", Toast.LENGTH_LONG).show();
}
}
希望能幫助到你!!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.