Android: wrap GridView in a dialog
Does anyone know how to wrap a GridView?
Despite setting layout_width to wrap_content both on the GridView and parent LinearLayout the GridView is still bigger than its 3 column content (approx. double the content width). Tried also to apply fixed layout_width = ... dp. That did not work either.
Any ideas how to wrap the GridView? See below.
<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:gravi开发者_开发问答ty="center" />
</LinearLayout>
Setting the LinearLayout and the GridView to "wrap_contents" won't work, as the GridView pretty much defaults to "fill_parent" if you don't give it a specific size. I did some testing, and you have several solutions depending on how you create your view :
Setting the LinearLayout to android:layout_width="100dip"
View view = getLayoutInflater().inflate(R.layout.grid_dialog, null);
dialog.setContentView(view);
=> doesn't work as expected : the inflater doesn't have a parent view when inflating, so it just doesn't take all the android:layout_XXX attributes into account.
dialog.setContentView(R.layout.grid_dialog);
=> works as expected, because this time the dialog will do the inflate and know the parent view. You can use dialog.findViewById to retrieve the gridview if needed....
Setting the GridView to android:layout_width="100dip"
Apparently works all the time, as it has a parent when it's inflated, so the layout_XXX attributes actually work.
All this isn't perfect as it doesn't do a real wrap_content on the GridView and you have to manually set the layout_width, but it should work.
Thanks Gregory. I'm not sure how to do the dialog.setContentView(R.layout.grid_dialog) in an AlertDialog builder. The AlertView dialog has only SetView, not setContentView available. Any hints on how to inflate the AlertDialog so it knows its parent view? See the AlertDialog code I'm using below, that does not wrap the 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;
}
In addition the
View layout = inflater.inflate(R.layout.categorydialog_3_cols, (ViewGroup) findViewById(R.id.layout_root));
has the parent View (for the Grid view) defined as
(ViewGroup) findViewById(R.id.layout_root)
You can try setting the width at runtime, like this:
dialog.show();
LayoutParams params = dialog.getWindow().getAttributes();
params.width = 100;
dialog.getWindow().setAttributes(params);
Reference: How can I set the AlertDialog width, etc.
精彩评论