Android弹出 GridView

标签 android

可以使用 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/

相关文章:

android - 我的 robofragment 无法注入(inject) View (roboguice 2.0)

java - 在切换期间存储变量更改 [Java]

java - 我无法解决尝试在空对象引用上调用虚拟方法 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()'

java - 将 json 对象添加到 Java HttpGet 请求到 C# WebApi

android - bluetoothgatt onCharacteristicWrite 使用 ble 设备返回状态代码 6

android - 如何自定义APK中依赖项资源的放置位置?

java - IntentService 正在卡住我的应用程序 UI

java - 在 android 中使用 google map v2 在触摸位置添加标记

java - 当表情符号键盘显示时调整键盘大小或隐藏虚拟键盘

android - ImageView 自动链接