如何在Android警告对话框的顶部角落放置关闭按钮?

7
如何在Android的警告对话框中将关闭按钮放在顶部角落?
将关闭按钮放置在警告对话框右侧顶部角落。
我已经使用了下面的代码进行滚动和对话框盒子。
<ScrollView 
        android:id="@+id/ScrollView01" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        xmlns:android="http://schemas.android.com/apk/res/android">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:scrollbars="vertical" 
   android:scrollbarAlwaysDrawVerticalTrack="true" >

<ImageVieandroid:id="@+id/image"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentRight="true"
 android:layout_alignParentTop="true"
 android:layout_marginLeft="200dp"
 android:src="@drawable/ic_launcher" />

</RelativeLayout>

</ScrollView>

在 Java 编码中,我使用了以下代码,我想把一张图片放在对话框上以关闭它,请帮忙。

public void onClick(View v) 
{
// TODO Auto-generated method stub
   switch (v.getId())
   {
    case R.id.How_to_use:

 final Dialog dialog = new Dialog(this);
 dialog.setContentView(R.layout.description);
 dialog.setTitle("How to use");
 TextView text = (TextView) dialog.findViewById(R.id.description);
 text.setText(R.string.Descr_How_to_use);
 ImageView image = (ImageView) dialog.findViewById(R.id.image);
 image.setImageResource(R.drawable.ic_launcher);
 image.setOnClickListener(new OnClickListener() 
 {
  // @Override
  public void onClick(View v) {
  dialog.dismiss();
  }
  });
 dialog.show();
 break;
 default:
 break;
 }

1
希望这可以帮助:https://dev59.com/Vmox5IYBdhLWcg3ww3GV - Alexis C.
1
使用自定义对话框,在角落设置按钮,将其填充到对话框中并使用它。 - abhi
我已经创建了一个自定义对话框,但是我不知道如何将按钮设置在对话框的右上角,你能帮我吗? - Hardik Mer
请使用以下链接参考:https://dev59.com/BFXTa4cB1Zd3GeqPxwz3#22655042 - Amir Qayyum Khan
3个回答

15

我知道,回答这个两年前的问题有点晚了,但是这是为那些还不知道正确方法的人准备的...

使用自定义对话框(如大家所建议的)。

使用RelativeLayout作为custom_dialog.xml的主布局,因为你需要将取消按钮浮动在主窗口的右上角/左上角。

custom_dialog.xml

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="15dp"
        android:background="#F4F4F4">

        <!--Main Body of your custom dialog-->
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/llTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/btnCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btnBookK"
            android:background="@null"
            android:src="@drawable/ic_close" />

    </LinearLayout>

</RelativeLayout>
在您的自定义对话框代码中,请使用以下代码使其透明:
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

2

customDialog.xml

<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
>

<LinearLayout
android:id="@+id/llTop"    
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:paddingBottom="10dp"

>


<Button
    android:id="@+id/btnClose"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000" 
    android:text="Close"

    />

</LinearLayout>

 <ImageView
    android:src="@drawable/ic_launcher" 
    android:layout_width="130dp"
    android:layout_height="130dp"
    android:text="Close"
    android:layout_centerInParent="true"
    android:layout_below="@+id/llTop"
    />



</RelativeLayout>

我创建了一个方法,每当你想要显示对话框时,只需调用此方法。

private Dialog dialog; // class variable

private void showDialog
{
dialog = new Dialog(Activity.this);  // always give context of activity.
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.customDialog);


Button dialogButton = (Button) dialog.findViewById(R.id.btnClose);

        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) 
            {
                dialog.dismiss();
            }
        });

dialog.show();

}


您可以参考此链接以获得更清晰的解释。https://dev59.com/XmIj5IYBdhLWcg3wcEuI - trueblue

1

你必须创建自定义对话框

import android.app.Dialog;
import android.content.Context;

public class CustomDialog extends Dialog 
{

public CustomDialog(Context context) 
{
    super(context);
    setContentView(R.layout.dialog);
}

}

创建dialog.xml文件。根据您的需求进行设计。


2
这个被接受的答案没用,它并没有回答问题,只是展示了如何创建一个对话框片段。 - Shahid Sarwar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接