可以使用 GridView 创建弹出窗口。例如,我要单击一个按钮,然后会弹出一个带有 GridView 的窗口,然后我单击它传输到我的 ImageView 的其中一个项目,我尝试了很多教程,但没有一个适合我。我是 android 开发的新手,请帮助或任何建议或示例 fragment 提前谢谢你..
最佳答案
这是可能的。在我看来,最简单的方法是使用您想要的布局创建 DialogFragment 的子类
对话框看起来像这样
public class GridDialogFragment extends DialogFragment {
GridView mGridView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); // it title is not needed
View view = inflater.inflate(R.layout.grid_dialog, container); // you should define dialog layout in resources
mGridView = (GridView)view.findViewById(R.id.grid);
//initialize your gridview
// ...
return view;
}
//here can be other methods
}
您的布局可以如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/selectable_background_mainstyle"
android:orientation="vertical" >
<!-- here can be other elements -->
<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- here can be other elements -->
</LinearLayout>
你可以用下一种方式显示对话框:
GridDialogFragment fr = new GridDialogFragment();
fr.setTitle(file.getName()); //Set title if needed
fr.show(getFragmentManager(), "tag");
如果您的最低 sdk 级别低于 11,请不要忘记将支持库添加到您的项目中
关于Android弹出 GridView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21874816/