ListAdapter in Android 1.5 and 1.6: are there any differences?
I made an application that uses a ListAdapter
to display some items. In my getView
method I use LinearLayout#setPadding()
to define the indention of each view depending of some object specific stat开发者_运维知识库es.
This worked fine in Android 1.5. Last week I decided to move to 1.6 since it's better suitable for different screen sizes. It all worked fine, but the ListAdapter
looks really weird now. The padding isn't working correctly anymore.
Does anybody know if there is a difference in the ListAdapter
or setPadding
implementation between Android 1.5 and Android 1.6?
===EDIT: Code added===
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text = (TextView) View.inflate(lexs, R.layout.law_list_book_view, null);
if (items.get(position) instanceof Chapter) {
LinearLayout chapterView = new LinearLayout(lexs);
chapterView.setOrientation(LinearLayout.HORIZONTAL);
chapterView.setGravity(Gravity.CENTER);
ImageView img = new ImageView(lexs);
img.setPadding(0, 0, 5, 0);
chapterView.addView(img);
chapterView.addView(text);
text.setTextColor(Color.BLACK);
text.setText((CharSequence) items.get(position).getName() + ": " + ((Chapter) items.get(position)).getTitle());
text.setMinWidth(320);
switch (((Chapter) items.get(position)).getLevel()) {
case 0:
text.setTextColor(Color.rgb(0, 0, 0));
break;
case 1:
text.setTextColor(Color.rgb(50, 50, 50));
chapterView.setPadding(10, 0, 0, 0);
break;
case 2:
text.setTextColor(Color.rgb(100, 100, 100));
chapterView.setPadding(20, 0, 0, 0);
break;
case 3:
text.setTextColor(Color.rgb(150, 150, 150));
chapterView.setPadding(30, 0, 0, 0);
break;
case 4:
text.setTextColor(Color.rgb(175, 175, 175));
chapterView.setPadding(40, 0, 0, 0);
break;
default:
text.setTextColor(Color.rgb(200, 200, 200));
chapterView.setPadding(50, 0, 0, 0);
break;
}
if (((Chapter) items.get(position)).isExpanded()) {
img.setImageResource(R.drawable.minus);
} else {
img.setImageResource(R.drawable.plus);
}
if (((Chapter) items.get(position)).isSearched()) {
text.setBackgroundColor(Color.MAGENTA);
}
text.setTextSize(15);
return chapterView;
} else {
LinearLayout paragraphView = new LinearLayout(lexs);
paragraphView.setOrientation(LinearLayout.HORIZONTAL);
paragraphView.setGravity(Gravity.CENTER);
paragraphView.setPadding( (((Chapter) ((Paragraph) items.get(position)).getRoot()).getLevel() * 10) + 15 + 8 , 0, 0, 0);
DisplayMetrics metrics = new DisplayMetrics();
lexs.getWindowManager().getDefaultDisplay().getMetrics(metrics);
ImageView paragraphImg = new ImageView(lexs);
paragraphImg.setImageResource(R.drawable.paragraph);
paragraphView.addView(paragraphImg);
LinearLayout textLayer = new LinearLayout(lexs);
textLayer.setGravity(Gravity.LEFT);
textLayer.setMinimumWidth(metrics.widthPixels / 2);
text.setTextColor(Color.BLACK);
textLayer.addView(text);
textLayer.setOnClickListener(new BookViewOnClickListener(position));
paragraphView.addView(textLayer);
text.setText("Artikel " + (CharSequence) items.get(position).getName());
LinearLayout buttonLayer = new LinearLayout(lexs);
buttonLayer.setGravity(Gravity.RIGHT);
ImageButton mark = new ImageButton(lexs);
if (WorkspaceView.createView(lexs).isFavorite((Paragraph) items.get(position))) {
mark.setBackgroundResource(R.drawable.article_not_favorite);
} else {
mark.setBackgroundResource(R.drawable.article_favorit);
}
mark.setOnClickListener(new BookViewOnClickListener(position));
buttonLayer.addView(mark);
buttonLayer.setPadding(0, 0, 10, 0);
buttonLayer.setMinimumWidth(metrics.widthPixels / 2);
paragraphView.addView(buttonLayer);
text.setTextSize(15);
if (((Paragraph) items.elementAt(position)).isSearched()) {
text.setBackgroundColor(Color.RED);
}
return paragraphView;
}
}
===EDIT 2: Screenshot added===
I know that this elements of the list are all level 0 elements and should therefore have no padding (I used logcat to verify the level of the elements. they are all 0). the level of the elements i use in the switch case to check the padding (see code)
You have the API Differences page to check that but honestly I don't think it's a SDK issue. Try posting your code so we can reproduce.
EDIT: Screenshot after trying to reproduce the issue:
This looks good, what's your issue again?
You might want to check out the more friendly UI Framework Changes in Android 1.6.
精彩评论