-
先看看效果
选中后背景变白,字体变红,点击取消收起(取消可以换成确定)
控制台输出选中
-
以下是代码
– DialogFragment.java
package com.example.myfirstapp.web.fragment;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.SparseBooleanArray;
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.widget.AdapterView;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.myfirstapp.R;
import com.example.myfirstapp.web.adapter.DialogAdapter;
/**
* 底部弹框Fragment
*/
public class DialogFragment extends androidx.fragment.app.DialogFragment {
private String[] arrs = {
"看电影", "去唱歌", "吃吃喝喝", "旅游", "夜蒲聚会", "玩游戏", "运动", "其他",};
private DialogAdapter dialogAdapter;
private ListView listView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//去掉dialog的标题,需要在setContentView()之前
this.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = this.getDialog().getWindow();
//去掉dialog默认的padding
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
//设置dialog的位置在底部
lp.gravity = G