2011-07-22 37 views
1

即時通訊設法使我的圖像很好地適合屏幕而不重疊 但無論我做什麼,我都無法讓它們正確縮放。 我認爲ImageView.ScaleType.CENTER_CROP會縮放我的圖像,但可能我會做這一切都錯了。 任何幫助,將不勝感激。 (如果你知道簡單的功能,將解決我的問題,然後請隨時應對不讀我的代碼,其中大部分是教程代碼反正)android佈局;調整圖像的列,以便它們適合屏幕

這裏是我的了: 的main.xml文件:

<?xml version="1.0" encoding="utf-8"?> 

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:columnWidth="75dp" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="0dp" 
    android:horizontalSpacing="1dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
/> 

這是我HelloGrid.java文件:

package com.grid.example; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.GridView; 
import android.widget.Toast; 

public class HelloGrid extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); //removes annoying title on top 
      setContentView(R.layout.main); 

      GridView gridview = (GridView) 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(HelloGrid.this, "" + position, Toast.LENGTH_SHORT).show(); 
       } 
      }); 
    } 
} 

,這是我ImageAdapter.java文件:

package com.grid.example; 

import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 

public class ImageAdapter extends BaseAdapter { 
    private Context myContext; 

    public ImageAdapter(Context c) { 
     myContext = c; 
    } 

    public int getCount() { 
     return mThumbIds.length; 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

    // create a new ImageView for each item referenced by the Adapter 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 
     if (convertView == null) { // if it's not recycled, initialize some attributes 
      imageView = new ImageView(myContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(4, 0, 4, 0); 
     } else { 
      imageView = (ImageView) convertView; 
     } 

     imageView.setImageResource(mThumbIds[position]); 


     return imageView; 

    } 

    // references to our images 
private Integer[] mThumbIds = { 

      R.drawable.sample_0, R.drawable.sample_3, 
      R.drawable.sample_4, R.drawable.sample_5, 
      R.drawable.sample_6, R.drawable.sample_7, 
      R.drawable.sample_0, R.drawable.sample_1, 
      R.drawable.sample_2, R.drawable.sample_3, 
      R.drawable.sample_4, R.drawable.sample_5, 
      R.drawable.sample_6, R.drawable.sample_7, 
      R.drawable.sample_0, R.drawable.sample_1, 
      R.drawable.sample_2, R.drawable.sample_3, 
      R.drawable.sample_4, R.drawable.sample_5, 
      R.drawable.sample_6, R.drawable.sample_7, 
      R.drawable.sample_2, R.drawable.sample_3, 
      R.drawable.sample_4, R.drawable.sample_5, 
      R.drawable.sample_6, R.drawable.sample_7, 
      R.drawable.sample_0, R.drawable.sample_1, 
      R.drawable.sample_2, R.drawable.sample_3, 
      R.drawable.sample_4, R.drawable.sample_5, 
      R.drawable.sample_6, R.drawable.sample_7, 
      R.drawable.sample_0, R.drawable.sample_1, 
      R.drawable.sample_2, R.drawable.sample_3, 
      R.drawable.sample_4, R.drawable.sample_5, 
      R.drawable.sample_6, R.drawable.sample_7, 
      R.drawable.sample_2, R.drawable.sample_3, 
      R.drawable.sample_4, R.drawable.sample_5 
      }; 

} 

我嘗試編輯main.xml文件中的填充和horizo​​ntalspacing。 我得到的垂直間距很好。我只是有圖像的問題來調整大小,所以他們完美適合屏幕。

回答

0

在代碼中使用硬編碼「圖像佈局」的選項是爲ImageView創建新的佈局。然後,在代碼中膨脹佈局。

這樣做的好處是,您可以使用graphiclaUI編輯器查看屬性,查看ImageView行爲方式的預覽,並且可以更輕鬆地在不修改代碼的情況下修改行爲。 (您可能如希望有其它屏幕尺寸不同的佈局)

更新: 你可以找到如何膨脹這裏看一個例子: API Demos, List14

+0

感謝您的回覆。我有困難的時間,但不管理解硬編碼是什麼意思?你的意思是「編程式」UI佈局嗎?以及在代碼中誇大佈局意味着什麼。也許如果你有一些基本的佈局代碼來顯示我會更容易理解。 – Xitcod13

+0

我更新了我的回覆,並給出了一個擴展布局的例子。 – Kaj

0

一次嘗試這個

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/gridview" 
     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" 
    /> 
+0

感謝您提供實際的代碼:)這會阻止圖像重疊,但是我想讓圖像彼此緊挨着而不會重疊。 – Xitcod13

0

由於Kaj已經在上面回答了,我完全同意他的看法。僅使用ImageView創建行佈局文件,然後在ImageAdapter類中膨脹相同的佈局。

這種行xml佈局的主要優點是預覽佈局。

而在創建ImageView時,請確保您定義了android:scaleType="center"以便它顯示在中心區域。

+0

感謝您的幫助。我在哪裏可以找到佈局的預覽? – Xitcod13

+0

@ Xitcod13在設計佈局時沒有得到預覽嗎? –

+0

我得到了它的感謝。我只是用來手動編輯XML文件,我忘了其他標籤甚至在那裏 – Xitcod13