1. 程式人生 > >android中用DialogFragment實現底部彈框

android中用DialogFragment實現底部彈框

效果圖

img

程式碼

需要注意的地方用註釋給出。

可以在github上下載原始碼。點我試試

Dialog2

Dialog2繼承自DialogFragment

package 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");

參考文章

原文