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;
 }
4

3 に答える 3

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>

ダイアログを表示したいときはいつでも、このメソッドを呼び出すだけで1つのメソッドを作成しました。

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();

}

于 2013-06-18T13:26:01.607 に答える