2017-10-04 87 views
0

我想在我的alertdialog中添加图像。 Android将它放置在文本下面,它很好看,但它增加了一些无用的余量。 如何取代它?AlertDialog SetView图像大小

我的观点:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/rate"/> 
</LinearLayout> 

我的警告对话框:

mAlertDialogBuilder.setCancelable(true) 
       .setView(getLayoutInflater().inflate(R.layout.rate_app_dialog, null)) 
       .setIcon(R.mipmap.ic_launcher) 
       .setTitle(R.string.exiting_the_application) 
       .setMessage(R.string.rate_app_dialog) 
       .show(); 

enter image description here

+0

为什么不创建自定义的'DialogFragment'并使用它。您可以按照自己想要的方式制作自定义对话框片段。 – Barns

回答

0

您可以更改下面的代码,你需要的任何方式。这只是您如何制作和实施自定义DialogFragment的示例。

他是我使用的代码。我发现它非常灵活,因为您可以创建几个类似的对话框,用于稍微不同的任务。您将需要创建一个布局文件 - 这会为您提供很大的功能和风格灵活性。

我的布局文件是fragment_ok_cancel_dialog。

为了满足您的要求,只需创建您自己的布局文件,其中包含所需的所有元素(如图像)。

在调用对话框的Activity中,您需要实现Listener。

implements OkCancelDialogFragment.OkCancelDialogListener 

另一个优点是与我的代码,你可以更改标题和消息,以适应任何活动的需要。

private void callMyDialog(){ 
    //Customize the title and message as needed 
    String title = "This is my dialog title"; 
    String mess = "This is my dialog message"; 
    OkCancelDialogFragment dialog = OkCancelDialogFragment.newInstance(title, mess); 
    dialog.show(getFragmentManager(), "OkCancelDialogFragment2"); 
} 

现在您需要在调用DialogFragment的Activity中实现对话回调。

@Override 
public void onFinishOkCancelDialog(boolean submit) { 
    if(submit){ 
     // Do something positive 
    } 
    else{ 
     // Do something negative 
    } 
} 

现在的DialogFragment代码:

public class OkCancelDialogFragment extends DialogFragment { 

    private static final String ARG_TITLE = "title"; 
    private static final String ARG_MESSAGE = "message"; 

    Context context = null; 

    private String title; 
    private String message; 
    private boolean submitData = false; 

    private OkCancelDialogListener mListener; 

    public OkCancelDialogFragment() { 
    } 

    public static OkCancelDialogFragment newInstance(String title, String message) { 
     OkCancelDialogFragment fragment = new OkCancelDialogFragment(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_TITLE, title); 
     args.putString(ARG_MESSAGE, message); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      title = getArguments().getString(ARG_TITLE); 
      message = getArguments().getString(ARG_MESSAGE); 
     } 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle saveIntsanceState){ 

     context = getActivity(); 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

     LayoutInflater inflater = getActivity().getLayoutInflater(); 

     View rootView = inflater.inflate(R.layout.fragment_ok_cancel_dialog, null, false); 
     final TextView titleView = (TextView)rootView.findViewById(R.id.tvTitle); 
     final TextView messView = (TextView)rootView.findViewById(R.id.tvMessage); 

     titleView.setText(title); 
     messView.setText(message); 

     builder.setView(rootView) 
//    .setTitle(title) 
       .setPositiveButton(R.string.ok_button_dialog_title, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 

         submitData = true; 
         if(mListener == null) mListener = (OkCancelDialogListener) context; 
         mListener.onFinishOkCancelDialog(submitData); 
        } 
       }) 
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         submitData = false; 
         if(mListener == null) mListener = (OkCancelDialogListener) context; 
         mListener.onFinishOkCancelDialog(submitData); 
        } 
       }); 
     return builder.create(); 
    } 


    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     try { 
      if(mListener == null) mListener = (OkCancelDialogListener) context; 
     } 
     catch (Exception ex){ 
      throw new RuntimeException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 


    public interface OkCancelDialogListener { 
     void onFinishOkCancelDialog(boolean submit); 
    } 

} 

请注意,.setTitle(标题)是有效的API 23或更高版本(或者API 21或更高?)。