【问题标题】:Android: wrap GridView in a dialogAndroid:在对话框中包装 GridView
【发布时间】:2011-06-24 01:32:30
【问题描述】:

有谁知道如何包装 GridView?

尽管在 GridView 和父 LinearLayout 上都将 layout_width 设置为 wrap_content,但 GridView 仍然大于其 3 列内容(大约是内容宽度的两倍)。还尝试应用固定的 layout_width = ... dp。那也没用。

任何想法如何包装 GridView?见下文。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
          android:id="@+id/layout_root"   
          android:orientation="vertical"   
          android:layout_width="wrap_content"   
          android:layout_height="wrap_content"   
          android:padding="10dp" >

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"    
        android:id="@+id/gridview"   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        android:columnWidth="90dp"   
        android:numColumns="3"   
        android:verticalSpacing="10dp"   
        android:horizontalSpacing="10dp"   
        android:stretchMode="none"   
        android:gravity="center" />

</LinearLayout>   

【问题讨论】:

    标签: android gridview android-layout


    【解决方案1】:

    将 LinearLayout 和 GridView 设置为“wrap_contents”是行不通的,因为如果您不给它一个特定的大小,GridView 几乎默认为“fill_parent”。 我做了一些测试,根据您创建视图的方式,您有几种解决方案:

    将 LinearLayout 设置为 android:layout_width="100dip"

    View view = getLayoutInflater().inflate(R.layout.grid_dialog, null);
    dialog.setContentView(view);
    

    => 无法正常工作:inflater 在充气时没有父视图,因此它只是没有考虑所有 android:layout_XXX 属性。

        dialog.setContentView(R.layout.grid_dialog);
    

    => 按预期工作,因为这次对话框将进行膨胀并知道父视图。如果需要,您可以使用 dialog.findViewById 来检索 gridview....

    将 GridView 设置为 android:layout_width="100dip"

    显然一直有效,因为它在膨胀时有一个父节点,所以 layout_XXX 属性确实有效。

    所有这些都不是完美的,因为它没有在 GridView 上执行真正的 wrap_content,您必须手动设置 layout_width,但它应该可以工作。

    【讨论】:

      【解决方案2】:

      谢谢格雷戈里。我不确定如何在 AlertDialog 构建器中执行 dialog.setContentView(R.layout.grid_dialog) 。 AlertView 对话框只有 SetView,没有 setContentView 可用。关于如何膨胀 AlertDialog 以使其知道其父视图的任何提示?请参阅下面我使用的 AlertDialog 代码,它不包含 GridView:

      protected Dialog onCreateDialog(int id) {   
          switch(id) {   
          case CATEGORY_ID:   
                  AlertDialog.Builder builder;   
                  Context mContext = this;   
                  LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);   
                  View layout = inflater.inflate(R.layout.categorydialog_3_cols, (ViewGroup) findViewById(R.id.layout_root));   
                  GridView gridview = (GridView) layout.findViewById(R.id.gridview);   
                  gridview.setAdapter(new ImageAdapter(this));   
                  gridview.setOnItemClickListener(new OnItemClickListener() {   
                  public void onItemClick(AdapterView<?> parent, View v, int position, long id) {   
                      Toast.makeText(FS_90x90.this, "" + position, Toast.LENGTH_SHORT).show();   
                  }   
              });   
                  builder = new AlertDialog.Builder(mContext);   
                  builder.setView(layout);   
                  dialog = builder.create();   
              break;   
          default:   
              dialog = null;   
          }   
          return dialog;   
      }   
      

      除了

      视图布局 = inflater.inflate(R.layout.categorydialog_3_cols, (ViewGroup) findViewById(R.id.layout_root));

      将父视图(用于网格视图)定义为

      (ViewGroup) findViewById(R.id.layout_root)

      【讨论】:

        【解决方案3】:

        您可以尝试在运行时设置宽度,如下所示:

        dialog.show();
        LayoutParams params = dialog.getWindow().getAttributes();
        params.width = 100;
        dialog.getWindow().setAttributes(params);
        

        参考:How can I set the AlertDialog width, etc.

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-10-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多