(Android的布局)的GridView与图像和文本增加了extraspace各地图片 [英] (android-layout) GridView with Image and Text adds extraspace around Image

查看:146
本文介绍了(Android的布局)的GridView与图像和文本增加了extraspace各地图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个gridview处理图像并超过他们相应的文本。它几乎没有工作,但我周围的一些图像extraspace。我首先想到的空间将低于TextView的UND的ImageView的,但在取出文本时,同样的空间一直存在。我读,我必须添加机器人:adjustViewBounds =true将我的ImageView删除此extraspace,但比我的应用程序崩溃。谁能帮我?当一般的碰撞做了布局?

下面是griditems的XML

single_grid_item.xml:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:ID =@ + ID / single_item_id
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =FILL_PARENT
  机器人:方向=垂直
  >
      <的TextView
      机器人:ID =@ + ID / IMAGE_NAME
      机器人:layout_width =FILL_PARENT
      机器人:layout_height =WRAP_CONTENT
      />
      < ImageView的
      机器人:ID =@ + ID / album_image
      机器人:adjustViewBounds =真
      机器人:layout_width =FILL_PARENT
      机器人:layout_height =WRAP_CONTENT
      /> < / LinearLayout中>

这是完整的Sreenlayout的XML。

home_1.xml:

 < RelativeLayout的
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直
  >
  <的LinearLayout
  机器人:layout_alignParentTop =真
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =FILL_PARENT
  机器人:方向=垂直>
    < GridView控件
    机器人:ID =@ + ID / home_1
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:columnWidth时=90dp
    机器人:为numColumns =auto_fit
    机器人:verticalSpacing =10dp
    机器人:horizo​​ntalSpacing =10dp
    机器人:stretchMode =columnWidth时
    机器人:比重=中心
    />
    < / LinearLayout中>
    <的LinearLayout
        机器人:ID =@ + ID / home_1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真
        机器人:layout_below =@ + ID / GridView控件
        机器人:背景=#231f20
        机器人:可聚焦=真
    >   <按钮
   机器人:ID =@ + ID / home1searchButton
   机器人:文字=
   机器人:layout_width =50dp
   机器人:layout_height =50dp
   机器人:背景=@绘制/放大
   机器人:可聚焦=真
   />
    <按钮
   机器人:ID =@ + ID / home1loadButton
   机器人:文字=
   机器人:layout_width =WRAP_CONTENT
   机器人:layout_height =WRAP_CONTENT
   机器人:背景=@绘制/ adjustmt
   机器人:可聚焦=真   />
   <按钮
   机器人:ID =@ + ID / home1editButton
   机器人:文字=
   机器人:layout_width =WRAP_CONTENT
   机器人:layout_height =WRAP_CONTENT
   机器人:背景=@绘制/辅助
   机器人:可聚焦=真
   />
   <按钮
   机器人:ID =@ + ID / home1sendButton
   机器人:文字=
   机器人:layout_width =WRAP_CONTENT
   机器人:layout_height =WRAP_CONTENT
   机器人:背景=@绘制/收音机
   机器人:可聚焦=真
   />
   <按钮
   机器人:ID =@ + ID / home1folderupButton
   机器人:文字=
   机器人:layout_width =WRAP_CONTENT
   机器人:layout_height =WRAP_CONTENT
   机器人:背景=@绘制/ folderup
   机器人:可聚焦=真
   />
    <按钮
   机器人:ID =@ + ID / home1newfoldrButton
   机器人:文字=
   机器人:layout_width =WRAP_CONTENT
   机器人:layout_height =WRAP_CONTENT
   机器人:背景=@绘制/ newfoldr
   机器人:可聚焦=真
   />
    <按钮
   机器人:ID =@ + ID / home1trashButton
   机器人:文字=
   机器人:layout_width =WRAP_CONTENT
   机器人:layout_height =WRAP_CONTENT
   机器人:背景=@绘制/垃圾
   机器人:可聚焦=真
   />
    <按钮
   机器人:ID =@ + ID / home1yesButton
   机器人:文字=
   机器人:layout_width =WRAP_CONTENT
   机器人:layout_height =WRAP_CONTENT
   机器人:背景=@绘制/是
   机器人:可聚焦=真
   />
< / LinearLayout中>

有关填充GridView的,我用我的ImageAdapter,它扩展baseadapter。这是它的getVIew功能:

  @覆盖公共查看getView(INT位置,查看convertView,父母的ViewGroup)
    {        ImageView的图标;
        图标=新ImageView的(mContext);        LayoutInflater吹气=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        查看排= inflater.inflate(R.layout.single_griditem,父母,假);
        TextView的标签=(TextView中)row.findViewById(R.id.image_name);
        label.setText(fileNameArray.get(位置));
        图标=(ImageView的)row.findViewById(R.id.album_image);        icon.setImageBitmap(bmArray.get(位置));        返回行;
    }

这是我的onCreate-功能

  super.onCreate(savedInstanceState);
        的setContentView(R.layout.home_1);        的String []一个= NULL;        ImageAdapter mAdapter =新ImageAdapter(本);        位图mBitmap = NULL;
        的for(int i = 0; I< 8;我++)
        {
            mBitmap = BitmapFactory.de codeResource(getResources(),R.drawable.a1 + I);
            ImageAdapter.addImage(mBitmap,样本_+ 1 +JPG);
        }
        GridView控件的GridView =(GridView控件)findViewById(R.id.home_1);
        gridview.setAdapter(mAdapter);
        registerForContextMenu(GridView控件);
}


解决方案

我有同样的问题:<一href=\"http://stackoverflow.com/questions/7158446/viewflipper-not-wrapping-around-images-correctly/7158739#7158739\">ViewFlipper不缠绕图像正确的的android:adjustViewBounds =真正的的为我工作。
尝试使用的 setAdjustViewBounds(真)的,而不是在XML设置它。如果在做布局图像源是没有准备好,将ImageView的无界限适应,所以应用程序可能崩溃。

I want a Gridview with Images and corresponding Text over them. It nearly does work, but there is some extraspace around my images. I first thought the space would be below textview und the imageview, but when removing the text, same space has been there. I read that I would have to add android:adjustViewBounds="true" to my imageview to remove this extraspace, but than my App crashes. Can anyone help me? When does a layout in general crash?

Here is the xml of the griditems

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>

This is the XML for the complete Sreenlayout.

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>

For filling the Gridview, I use my ImageAdapter, which extends baseadapter. This is its getVIew Function:

@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;
    }

And this is my 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);
}

解决方案

I was having the same problem: ViewFlipper not wrapping around images correctly android:adjustViewBounds="true" worked for me. Try using setAdjustViewBounds(true) instead of setting it up in the XML. If the image source is not ready while doing the layout, the ImageView will have no bounds to adjust to, so the app could crash.

这篇关于(Android的布局)的GridView与图像和文本增加了extraspace各地图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆