我想要一个带有图像和相应文本的Gridview。它几乎可以工作,但我的图像周围有一些额外的空间。我首先想到的是空间位于textview和imageview之下,但是当删除文本时,同样的空间就在那里。我读到我必须在我的imageview中添加android:adjustViewBounds =“true”来删除这个额外空间,但是我的应用程序崩溃了。谁能帮我?布局何时崩溃?
这是griditems的xml
single_grid_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/single_item_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id = "@+id/image_name"
android:layout_width = "fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id = "@+id/album_image"
android:adjustViewBounds="true"
android:layout_width = "fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
这是完整的Sreenlayout的XML。
home_1.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<GridView
android:id="@+id/home_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/home_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/gridview"
android:background="#231f20"
android:focusable = "true"
>
<Button
android:id="@+id/home1searchButton"
android:text=""
android:layout_width="50dp"
android:layout_height="50dp"
android:background = "@drawable/magnify"
android:focusable = "true"
/>
<Button
android:id="@+id/home1loadButton"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background = "@drawable/adjustmt"
android:focusable = "true"
/>
<Button
android:id="@+id/home1editButton"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background = "@drawable/assist"
android:focusable = "true"
/>
<Button
android:id="@+id/home1sendButton"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background = "@drawable/radio"
android:focusable = "true"
/>
<Button
android:id="@+id/home1folderupButton"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background = "@drawable/folderup"
android:focusable = "true"
/>
<Button
android:id="@+id/home1newfoldrButton"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background = "@drawable/newfoldr"
android:focusable = "true"
/>
<Button
android:id="@+id/home1trashButton"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background = "@drawable/trash"
android:focusable = "true"
/>
<Button
android:id="@+id/home1yesButton"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background = "@drawable/yes"
android:focusable = "true"
/>
</LinearLayout>
为了填充Gridview,我使用了我的ImageAdapter,它扩展了baseadapter。这是它的getVIew函数:
@Override public View getView(int position, View convertView, ViewGroup parent)
{
ImageView icon;
icon = new ImageView(mContext);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.single_griditem, parent, false);
TextView label=(TextView)row.findViewById(R.id.image_name);
label.setText(fileNameArray.get(position));
icon=(ImageView)row.findViewById(R.id.album_image);
icon.setImageBitmap(bmArray.get(position));
return row;
}
这是我的onCreate-Function
super.onCreate(savedInstanceState);
setContentView(R.layout.home_1);
String [] a = null;
ImageAdapter mAdapter = new ImageAdapter(this);
Bitmap mBitmap = null;
for(int i = 0; i < 8; i++)
{
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a1 + i );
ImageAdapter.addImage(mBitmap, "sample_"+1+".jpg");
}
GridView gridview = (GridView) findViewById(R.id.home_1);
gridview.setAdapter(mAdapter);
registerForContextMenu(gridview);
}
答案 0 :(得分:1)
我遇到了同样的问题:ViewFlipper not wrapping around images correctly android:adjustViewBounds =“true”为我工作。 尝试使用 setAdjustViewBounds(true),而不是在XML中进行设置。如果在进行布局时图像源未准备就绪,则ImageView将无法调整范围,因此应用程序可能会崩溃。