2012-02-05 105 views
0

我的問題似乎很容易,但我真的無法解決它。在某個時候,我的代碼在AlertDialog內的LinearLayout中顯示了一堆圖像(ImageView)。 問題是,並非所有內容都顯示出來。LinearLayout將不會顯示所有內容

這裏是我的代碼:

public LinearLayout createLayout(String text) { 
    LinearLayout ll = new LinearLayout(this.c); 

    int res; 
    for(int i=0; i<text.length(); ++i) { 
     res = this.c.getResources().getIdentifier("pigpen_" + Character.toLowerCase(text.charAt(i)), "drawable", this.c.getPackageName()); 
     ImageView iv = new ImageView(this.c); 
     iv.setPadding(5, 0, 0, 5); 
     iv.setImageResource(res); 
     ll.addView(iv); 
    } 
    return ll; 
} 

而對於DialogAlert代碼:

protected ResultDialog(final Context context, CharSequence title, LinearLayout ll) { 
    super(context); 

    ll.setPadding(10, 5, 10, 5); 

    this.setView(ll); 
    this.setTitle(title); 
} 

image

正如你所看到的,結果並不好。你有什麼想法,爲什麼?

回答

0
LinearLayout ll = new LinearLayout(this.c); 
    ArrayList<Integer> alImages = new ArrayList<Integer>(); 
    ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

    for(int i=0; i<text.length(); ++i) { 
     cur = text.charAt(i); 
     pos = this.getCharAlphabetPosition(cur); 
     if(pos > 0 && pos < alphabetId.length) { 
      res = alphabetId[pos]; 
      alImages.add(res); 
     } 
    } 
    gv.setAdapter(new ImageAdapter(this.c, alImages)); 
    gv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

    gv.setNumColumns(GridView.AUTO_FIT); 
    gv.setColumnWidth(25); 

    ll.addView(gv); 
} 

的Et毫安CLASSE ImageAdapter: 公共類ImageAdapter延伸BaseAdapter實現ListAdapter { 私有上下文℃; private ArrayList alImages;

public ImageAdapter(Context c, ArrayList<Integer> alImages) { 
    this.c = c; 
    this.alImages = alImages; 
} 

public int getCount() { 
    return this.alImages.size(); 
} 

public Integer getItem(int position) { 
    return this.alImages.get(position); 
} 

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

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(this.c); 
     imageView.setLayoutParams(new GridView.LayoutParams(25, 25)); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     imageView.setPadding(5, 0, 0, 5); 
    } else { 
     imageView = (ImageView) convertView; 
    } 

    imageView.setImageResource(this.getItem(position)); 
    return imageView; 
} 

} 
0

問題是您的LinearLayout沒有任何尺寸。寬度和高度是0.所以一切都完美。

您缺少setLayoutParams的電話,就這些。

ll.setLayoutParams(new LinearLayout.LayoutParams(FILL_PARENT, FILL_PARENT)); 
+0

嗨。感謝您的幫助。我試過你的解決方案,但沒有任何改變。我也試圖把最低限度:佈局是一個很好的高度,但圖像仍然不是全部顯示。完美的行爲將是每種圖像都有一些回車。你認爲使用RelativeLayout會更好嗎? – Passeur 2012-02-05 18:13:34

+0

你可能會搜索'GridView'。 – poitroae 2012-02-05 19:11:52

+0

我已經使用了一個TableLayout,每10張圖片就有一個新的tableRow。我只關心它在大屏幕上的表現方式。 – Passeur 2012-02-07 09:39:06

0

您可以使用Horizo​​ntalScrollView包裹您的LinearLayout。這樣,您可以添加大量圖像並滾動查看全部圖像。