【发布时间】:2014-07-30 00:13:33
【问题描述】:
我想要一个带有图像和相应文本的 Gridview。它几乎可以工作,但我的图像周围有一些额外的空间。我首先认为该空间将在 textview und imageview 下方,但是当删除文本时,相同的空间已经存在。我读到我必须将 android:adjustViewBounds="true" 添加到我的 imageview 以删除这个多余的空间,但是我的应用程序崩溃了。谁能帮我?一般布局什么时候崩溃?
这里是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);
}
【问题讨论】:
标签: android-layout