我做了一个使用
ListAdapter
显示某些项目。在我的
getView
我使用的方法
LinearLayout#setPadding()
根据某些特定于对象的状态定义每个视图的缩进。
列表适配器
列表适配器
或
setPadding
android1.5和android1.6之间的实现?
===编辑:添加代码===
@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;
}
}