android中用DialogFragment实现底部弹框
2016-12-01 17:12
106 查看
效果图
代码
需要注意的地方用注释给出。可以在github上下载源码。点我试试。
Dialog2
Dialog2继承自DialogFragmentpackage com.qefee.pj.testdialogfragment.dialog; import android.app.DialogFragment; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.content.ContextCompat; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.Toast; import com.qefee.pj.testdialogfragment.R; public class Dialog2 extends DialogFragment { private static final String TAG = "Dialog2"; public static Dialog2 newInstance() { return new Dialog2(); } @Override public void onStart() { super.onStart(); Log.i(TAG, "onStart: "); Window window = getDialog().getWindow(); WindowManager.LayoutParams params = window.getAttributes(); params.gravity = Gravity.BOTTOM; // 显示在底部 params.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度填充满屏 window.setAttributes(params); // 这里用透明颜色替换掉系统自带背景 int color = ContextCompat.getColor(getActivity(), android.R.color.transparent); window.setBackgroundDrawable(new ColorDrawable(color)); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.i(TAG, "onCreateView: "); getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); // 不显示标题栏 final View dialogView = inflater.inflate(R.layout.dialog_2, container, false); Button okButton = (Button) dialogView.findViewById(R.id.okButton); Button cancelButton = (Button) dialogView.findViewById(R.id.cancelButton); okButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(getActivity(), "OK clicked", Toast.LENGTH_SHORT).show(); startDownAnimation(dialogView); } }); cancelButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(getActivity(), "Cancel clicked", Toast.LENGTH_SHORT).show(); startDownAnimation(dialogView); } }); startUpAnimation(dialogView); return dialogView; } private void startUpAnimation(View view) { Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f); slide.setDuration(400); slide.setFillAfter(true); slide.setFillEnabled(true); view.startAnimation(slide); } private void startDownAnimation(View view) { Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f); slide.setDuration(400); slide.setFillAfter(true); slide.setFillEnabled(true); slide.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { dismiss(); } @Override public void onAnimationRepeat(Animation animation) { } }); view.startAnimation(slide); } }
dialog_2
很简单的样式文件<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/colorAccent"> <TextView android:id="@+id/titleTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="this is title" /> <TextView android:id="@+id/contentTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="this is content" /> <Button android:id="@+id/okButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="OK" /> <Button android:id="@+id/cancelButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="cancel" /> </LinearLayout>
使用方法
Dialog2.newInstance().show(getFragmentManager(), "dialogTag2");
参考文章
使用DialogFragment实现底部菜单Android 官方推荐 : DialogFragment 创建对话框
详细解读DialogFragment
原文
android中用DialogFragment实现底部弹框相关文章推荐
- 【Android UI设计与开发】第08期:底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
- 【Android UI设计与开发】第08期:底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
- 【Android UI设计与开发】第08期:底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
- 【Android UI设计与开发】第08期:底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
- 【Android UI设计与开发】5.底部菜单栏(二)使用Fragment实现底部菜单栏
- android底部选项卡(二)FragmentTabHost +Fragment 实现
- Android博客挑错系列之一FragmentTabHost和ViewPager实现底部导航栏
- (4.1.8.3)【Android UI设计与开发】第08期:底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
- 【Android UI设计与开发】4.底部菜单栏(一)Fragment介绍和简单实现
- 【Android UI】RadioGroup+Fragment实现底部菜单栏
- 【Android UI设计与开发】第08期:底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
- Android 使用DialogFragment 对话框实现
- Android知识点九(ViewPager + Fragment 实现底部菜单栏)
- Android Fragment实现底部导航
- Android TabHost +Fragment 实现底部菜单栏
- Android 利用ViewPager实现底部圆点导航左右滑动效果以及Fragment页面切换
- 【Android UI设计与开发】第08期:底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
- Android工作笔记_Fragment底部几个按钮做主页的实现技术点
- Android下利用Fragment+RadioGroup和TabHost实现底部选项卡的效果
- 【Android UI设计与开发】4.底部菜单栏(一)Fragment介绍和简单实现