如何在带有网格的BottomSheetDialog上设置标题?

时间:2018-11-21 18:32:46

标签: java android customdialog

我已经用BottomSheetDialog创建了一个GridView,但是我知道BottomSheetDialog不能使用方法.setTitle()来设置标题,因此如何设置对话框上的标题?

这是我的代码:

活动:

    customAdapter = new CustomAdapter(this);

    GridView gridView = new GridView(this);
    gridView.setAdapter(customAdapter);
    gridView.setNumColumns(5);

    BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
    bottomSheetDialog.setContentView(gridView);
    bottomSheetDialog.show();

CustomAdapter:

    private final LayoutInflater inflater;
    private List<String> sizeCodes;

    public CustomAdapter(Context context) {
        this.sizeCodes = Collections.emptyList();
        this.inflater = LayoutInflater.from(context);
    }

    public CustomAdapter addItems(List<SizeData> items) {
        sizeCodes = new ArrayList<>();
        for (SizeData item : items)
            sizeCodes.add(item.getSize());
        notifyDataSetChanged();
        return this;
    }

    @Override
    public int getCount() {
        return sizeCodes.size();
    }

    @Override
    public String getItem(int position) {
        return sizeCodes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = inflater.inflate(R.layout.item_size_picker, parent, false);
        ((TextView) convertView.findViewById(R.id.content)).setText(sizeCodes.get(position));
        return convertView;
    }

item_size_picker:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <TextView
        android:id="@+id/content"
        style="@style/Base.TextAppearance.AppCompat.Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

</FrameLayout>

0 个答案:

没有答案