(android-layout) GridView с изображением и текстом добавляет дополнительное пространство вокруг изображения - PullRequest
0 голосов
/ 02 июня 2011

Я хочу Gridview с изображениями и соответствующим текстом поверх них.Это почти работает, но вокруг моих изображений есть некоторое пространство.Сначала я подумал, что пространство будет ниже текста и изображения, но при удалении текста там было то же самое пространство.Я прочитал, что мне нужно добавить android: AdjustViewBounds = "true" в мой просмотр изображений, чтобы удалить этот лишний пробел, но мое приложение вылетает.Может кто-нибудь мне помочь?Когда в общем случае происходит сбой макета?

Вот 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>

Это XML для полного 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>

Для заполнения 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

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

1 Ответ

1 голос
/ 23 августа 2011

У меня была та же проблема: ViewFlipper неправильно оборачивал изображения android: AdjustViewBounds = "true" работал для меня.Попробуйте использовать setAdjustViewBounds (true) вместо настройки его в XML.Если источник изображения не готов во время выполнения макета, у ImageView не будет границ для настройки, поэтому приложение может аварийно завершить работу.

...