如何使用GridLayout创建对话框?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用GridLayout创建对话框?相关的知识,希望对你有一定的参考价值。
如何创建对话框,该对话框使用GridLayout,并且当用户单击此布局的项目然后单击“选择”按钮时。 GridLayout项包含一个ImageView。如何实现此对话框?
答案
GridView gridView = new GridView(this);
gridView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, your_list));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
// do something here
);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(gridView);
builder.setTitle("ALERT BOX WITH GRID");
builder.show();
尝试一下,这基于单列数据网格..
[如果您需要图像,请按照以下步骤操作,https://android-developers.googleblog.com/2012/05/using-dialogfragments.html
另一答案
1-制作所需的自定义布局
2-使myDialog类扩展DialogFragment并使它膨胀到您制作的布局中
在片段3中添加所需的功能
但是我使用gridview,gridlayout比较简单,但是我建议使用gridview
这里是Dialog类
public class myDialog extends DialogFragment implements dis_listener
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.my_dialog_layout,container,false);
GridView gv = view.findViewById(R.id.dialog_grid);
grid_adapt adapt = new grid_adapt(getActivity(),this);
gv.setAdapter(adapt);
return view;
@Override
public void listen()
this.dismiss();
grid_adapt:-
public class grid_adapt extends BaseAdapter
private Context cxt;
//pass the data you want
//private ArrayList<Integers> imgs
private dis_listener img_dismiss;
grid_adapt(Context context,dis_listener dismisser)
this.cxt = context;
this.img_dismiss = dismisser;
@Override
public int getCount()
//return the data length
//return imgs.length
return 3;
@Override
public Object getItem(int position)
//return imgs.get(position);
return position;
@Override
public long getItemId(int position)
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
viewHolder vh;
if(convertView == null)
convertView= LayoutInflater.from(cxt).inflate(R.layout.grid_item, parent, false);
vh = new viewHolder(convertView);
convertView.setTag(vh);
else
vh = (viewHolder) convertView.getTag();
vh.button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Toast.makeText(cxt,"Open",Toast.LENGTH_LONG).show();
);
vh.img.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
img_dismiss.listen();
);
return convertView;
static class viewHolder
private Button button;
private ImageView img;
viewHolder(View view)
button = view.findViewById(R.id.dialog_item_button);
img = view.findViewById(R.id.img);
该接口只有一个叫listen的方法
以上是关于如何使用GridLayout创建对话框?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 gridlayout 设置 OnClick android cardview 示例
Java,如何在 GridLayout 中水平和垂直交换组件的位置?
android Gridlayout 如何将所有图像对齐到 3 X 2 网格中?
如何正确使用 QQuickItem::stackBefore() 重新排序 GridLayout 中的项目?