2017-10-04 75 views
-1

这里是什么,我需要一个截图:有没有什么办法可以获得Google Apps中的Android对话框?

Link

我想添加到我的应用程序一个Dialbox这样,而不是默认的Android难看一个。

link

有没有谷歌图书馆(如材料?),让我做这件事,而无需重新编码的片段整个事情?

感谢

+0

创建自定义对话框 –

+0

有迹象表明,使得同样的对话框,你想许多图书馆。这些对话被称为材质对话框,我确信有一个支持库。如果你不想浏览代码,那么只需在你的style.xml文件中创建一个样式,并将样式传递给你的对话框。 – Umair

+0

@Umair我会看看库,感谢队友 –

回答

1

使用下表对话框。

MainActivity.java

import android.content.DialogInterface; 
import android.os.Build; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.design.widget.BottomSheetBehavior; 
import android.support.design.widget.BottomSheetDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

    LinearLayout backgroundLayout; 
    View bottomSheetView; 
    TextView textPrompt1, textPrompt2; 
    TextView textSDK; 
    BottomSheetDialog bottomSheetDialog; 
    BottomSheetBehavior bottomSheetBehavior; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     textPrompt1 = (TextView)findViewById(R.id.prompt1); 
     textPrompt2 = (TextView)findViewById(R.id.prompt2); 
     backgroundLayout = (LinearLayout)findViewById(R.id.backgroundlayout); 

     bottomSheetView = getLayoutInflater().inflate(R.layout.bottomsheetdialog_layout, null); 
     bottomSheetDialog = new BottomSheetDialog(MainActivity.this); 
     bottomSheetDialog.setContentView(bottomSheetView); 
     bottomSheetBehavior = BottomSheetBehavior.from((View) bottomSheetView.getParent()); 
     bottomSheetBehavior.setBottomSheetCallback(bottomSheetCallback); 

     bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() { 
      @Override 
      public void onShow(DialogInterface dialog) { 
       textPrompt1.setText("OnShow"); 
      } 
     }); 

     bottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { 
      @Override 
      public void onDismiss(DialogInterface dialog) { 
       bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); 
       textPrompt1.setText("OnDismiss"); 
      } 
     }); 

     backgroundLayout.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); 
       bottomSheetDialog.show(); 
      } 
     }); 

    } 

    BottomSheetBehavior.BottomSheetCallback bottomSheetCallback = 
      new BottomSheetBehavior.BottomSheetCallback(){ 
       @Override 
       public void onStateChanged(@NonNull View bottomSheet, int newState) { 
        switch (newState){ 
         case BottomSheetBehavior.STATE_COLLAPSED: 
          textPrompt2.setText("COLLAPSED"); 
          break; 
         case BottomSheetBehavior.STATE_DRAGGING: 
          textPrompt2.setText("DRAGGING"); 
          break; 
         case BottomSheetBehavior.STATE_EXPANDED: 
          textPrompt2.setText("EXPANDED"); 
          break; 
         case BottomSheetBehavior.STATE_HIDDEN: 
          textPrompt2.setText("HIDDEN"); 
          bottomSheetDialog.dismiss(); 
          break; 
         case BottomSheetBehavior.STATE_SETTLING: 
          textPrompt2.setText("SETTLING"); 
          break; 
         default: 
          textPrompt2.setText("unknown..."); 
        } 
       } 

       @Override 
       public void onSlide(@NonNull View bottomSheet, float slideOffset) { 

       } 
      }; 
} 

创建一个文件布局/ bottomsheetdialog_layout.xml,以限定BottomSheetDialog的布局。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="300dp" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:text="Bottom Sheet Dialog Example" 
      android:textSize="26dp" 
      android:textStyle="bold"/> 
     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@mipmap/ic_launcher"/> 

    </LinearLayout> 
</LinearLayout> 

输出:

enter image description here

+0

好!但是我必须自己设计风格,我想我会先看看GitHub库,看看我是否找到了一些看起来像的东西。无论如何感谢 –

+0

当然,你也可以尝试其他库。如果你可以根据需要设置风格,你可以修改bottomomsheetdialog_layout.xml文件,并从普通工具类中调用对话框。 –

0

你行你Dialog片段。

public class CustomDialog extends DialogFragment { 

    //region Variables 

    //options will be true if there is 2 option and false if there is a single option 
    private boolean options; 
    private WeakReference<DialogListener> mDialogListener; 

    //endregion 

    //region Constructors 

    public CustomDialog() { 
    } 

    //endregion 

    //region Instances 

    public static CustomDialog newInstance(WeakReference<DialogListener> listener) { 
     CustomDialog fragment = new CustomDialog(); 
     fragment.mDialogListener = listener; 
     return fragment; 
    } 

    //endregion 

    //region Lifecycle methods 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the Builder class for convenient dialog construction 
     Bundle bundle = getArguments(); 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     LayoutInflater inflater = getActivity().getLayoutInflater(); 
     View view =inflater.inflate(R.layout.custom_alert_layout,null); 
     builder.setView(view); 

     TextView title = view.findViewById(R.id.alert_dialog); 
     TextView positive = view.findViewById(R.id.positive); 
     TextView negative = view.findViewById(R.id.negative); 

     positive.setText("Ok"); 
     negative.setText("Cancel")); 
     title.setText("title"); 


     positive.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if(mDialogListener!=null) { 
        mDialogListener.get().onDialogPositiveClick(CustomDialog.this); 
       } 
       dismiss(); 
      } 
     }); 
     negative.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if(mDialogListener!=null) { 
        mDialogListener.get().onDialogNegativeClick(CustomDialog.this); 
       } 
       dismiss(); 
      } 
     }); 
     // Create the AlertDialog object and return it 
     return builder.create(); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     getDialog().setCanceledOnTouchOutside(false); 

    } 

    @Override 
    public void onDismiss(DialogInterface dialog) { 
     super.onDismiss(dialog); 
    } 

    //endregion 

    //region Interface 

    /** 
    * Interface to tranasfer the click to the Dialogs host 
    * The activity that creates an instance of this dialog fragment must 
    * implement this interface in order to receive event callbacks. 
    */ 
    public interface DialogListener { 
     void onDialogPositiveClick(DialogFragment dialog); 
     void onDialogNegativeClick(DialogFragment dialog); 
    } 

    //endregion 
} 

,并在任何你想要显示它

DialogFragment c = CustomDialog.newInstance(new WeakReference<CustomDialog.DialogListener>(this)); 
c.setArguments(bundle); 
c.show(getChildFragmentManager(), "dialogFrag"); 
相关问题