0

我正在尝试让自定义对话框工作。它应该没有标题、基本的 TextMessage 和我的自定义布局,通常会出现按钮。我尝试使用 AlertDialog.Builder、扩展 Dialog、调用 Dialog 上的方法来完成此操作,但仍然没有得到预期的结果。

这是布局,我喜欢用作自定义 ButtonArea (layout/dialog_footer):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <CheckBox
        android:id="@+id/dialog_checkbox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/never_show_again"
        android:textColor="@color/black"
        android:visibility="gone"
        android:background="@color/dialog_button_background"/>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/dialog_checkbox"
        android:layout_alignWithParentIfMissing="true"
        android:orientation="horizontal"
        android:background="@color/dialog_button_background"
        android:paddingBottom="-10dip">

        <Button
            android:id="@+id/dialog_btn_positive"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:text="@string/ok"
            android:gravity="center"
            android:visibility="gone"
            />

        <Button
            android:id="@+id/dialog_btn_negative"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:text="@string/btn_cancel" 
            android:visibility="gone" />
    </LinearLayout>

</RelativeLayout>

最简单的尝试是这样的:

AlertDialog.Buiilder builder = new AlertDialog.Builder(context);
builder.setMessage("my Message");
Dialog dialog = builder.create();

//builder has `setView for Message or setCustomTitle, but no setCustomFooter
LayoutInflater inflater = LayoutInflater.from(context);
View footer = inflater.inflate(R.layout.dialog_footer);

//either NullPointer Exception
dialog.addContentView(footer, footer.getLayoutParams);
//or AndroidRuntimeException (requestFeature() must be called befoire adding content)
dialog.addContentView(footer, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

如果我尝试通过在布局顶部放置 TextView 来模拟“MessageArea”,它可以工作,但底部有非常难看的黑色边框(我猜它是自定义对话框主题)。

保持系统对话框的“外观和感觉”的最佳方法是什么,但用我自己的视图替换按钮并自己处理任何事情?

4

2 回答 2

0

朋友您好,请查看此帖子,它可能对您的问题有所帮助

Android - 自定义 AlertDialog 背景颜色

在这里,我为我的应用程序构建了自己的自定义编辑对话框,请参阅布局代码

  <?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TableRow>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="Name: "
        android:textColor="#fff" />

    <EditText
        android:id="@+id/txtDelName"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp" />
</TableRow>

<TableRow>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="Contact No :"
        android:textColor="#fff" />

    <EditText
        android:id="@+id/txtDelAge"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:ems="10" >

        <requestFocus />
    </EditText>
</TableRow>

<TableRow android:layout_height="wrap_content" >

    <Spinner
        android:id="@+id/spinDiagDept"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:layout_marginLeft="5dp"
        android:layout_span="2"
        android:prompt="@string/Rel_prompt"
        android:visibility="visible" />
</TableRow>

</TableLayout>

这是警报对话框代码

    public class Alerts {

    public static AlertDialog ShowEditDialog(final Context con,final Emergencydb emp)
    {
AlertDialog.Builder b=new AlertDialog.Builder(con);
b.setTitle("Emergency Contact Details");
LayoutInflater li=LayoutInflater.from(con);
View v=li.inflate(R.layout.editdialog, null);

b.setIcon(android.R.drawable.ic_input_get);

b.setView(v);
final TextView txtName=(TextView)v.findViewById(R.id.txtDelName);
final TextView txtAge=(TextView)v.findViewById(R.id.txtDelAge);
final Spinner spin=(Spinner)v.findViewById(R.id.spinDiagDept);
Utilities.ManageDeptSpinner(con, spin);
for(int i=0;i<spin.getCount();i++)
{
    long id=spin.getItemIdAtPosition(i);
    if(id==emp.getDept())
    {
        spin.setSelection(i, true);
        break;
    }
}


txtName.setText(emp.getName());
txtAge.setText(String.valueOf(emp.getAge()));

b.setPositiveButton("Modify", new OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        emp.setName(txtName.getText().toString());
        emp.setAge(String.valueOf(txtAge.getText().toString()));                                    emp.setDept((int)spin.getItemIdAtPosition(spin.getSelectedItemPosition()));

        try
        {
        @SuppressWarnings("rawtypes")
        DatabaseHelper db=new DatabaseHelper(con);
        db.UpdateEmp(emp);
        Toast.makeText(getBaseContext(), "Modified Successfully",                       Toast.LENGTH_SHORT).show();
        }
        catch(Exception ex)
        {

        }
    }       
});

b.setNeutralButton("Delete", new OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        @SuppressWarnings("rawtypes")
        DatabaseHelper db=new DatabaseHelper(con);
        db.DeleteEmp(emp);

    }
});
b.setNegativeButton("Cancel", null);

return b.create();
//diag.show();

     }

    protected static Context getBaseContext() {
// TODO Auto-generated method stub
return null;
      }

        public static void ShowEmpAddedAlert(
    android.view.View.OnClickListener onClickListener) {
// TODO Auto-generated method stub

  }



}
于 2013-02-27T10:48:03.490 回答
0

在 Aleks GI 的帮助下,它成功了:

代码流程是这样的:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("MyMessage");

LayoutInflater inflater = LayoutInflater.from(context);
View footerView = inflater.inflate(R.layout.apo_plus_dialog_footer, null);

AlertDialog alertDialog = builder.create();
alertDialog.setView(footerView, 0,0,0,0);

if (!hasTitle){
    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
}

alertDialog.show();

以防万一有人想要和我有类似的 Dialog 行为。

于 2013-02-27T15:46:57.260 回答