Возникла небольшая проблема. Я хотел бы создать активность просмотра списка Android со всеми элементами в списке, имеющими фиксированную высоту.
Итак, мой элемент layout (thread _item.xml )выглядит так:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="300dip"
>
<TextView android:id="@+id/thread_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[dummy]"
android:textSize="20dip"
android:textStyle="bold"
/>
<ImageView android:id="@+id/thread_first_image"
android:layout_below="@id/thread_item_title"
android:scaleType="centerInside"
android:maxWidth="100dip"
android:maxHeight="100dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
<TextView
android:id="@+id/thread_item_preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/thread_first_image"
android:text="[dummy]"
android:textSize="15dip">
</TextView>
</RelativeLayout>
Я установил layout _высоту корневого элемента в 300dip и ожидаю, что все элементы будут иметь одинаковую высоту, но это не так. Когда я запускаю приложение, оно выглядит так, как будто высота имеет значение содержимого обтекания _.
Кроме того, само действие выглядит так:
public class ThreadListActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
Code to get items from the storage.
*/
setListAdapter(new ThreadItemAdapter(this, R.layout.thread_item, itemsArray)));
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
/*Start new Activity. Unrelated stuff.*/
}
});
}
}
И адаптер, который я использую, выглядит так:
public class ThreadItemAdapter extends ArrayAdapter<ThreadItem> {
Activity context;
List<ThreadItem> items;
public ThreadItemAdapter(Activity context, int textViewResourceId, List<ThreadItem> items) {
super(context, textViewResourceId, items);
this.context = context;
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inf = this.context.getLayoutInflater();
View result;
if (convertView == null) {
result = inf.inflate(R.layout.thread_item, null);
} else {
result = convertView;
}
TextView tbTitle = (TextView) result.findViewById(R.id.thread_item_title);
TextView tbPreview = (TextView) result.findViewById(R.id.thread_item_preview);
ImageView ivFirstImage = (ImageView) result.findViewById(R.id.thread_first_image);
ThreadItem item = items.get(position);
tbTitle.setText(item.getThreadTitle());
ivFirstImage.setImageBitmap(item.getFirstImage());
SimpleLeadingMarginSpan span = new SimpleLeadingMarginSpan(item.getFirstImage() != null ? 5 : 0, 115); // TODO: const
SpannableString previewText = new SpannableString(item.getThreadPreview());
previewText.setSpan(span, 0, previewText.length(), 0);
tbPreview.setText(previewText);
return result;
}
}
Я не понимаю, почему все элементы списка по-прежнему оборачивают свое содержимое и не остаются на 300 градусов по высоте. Они могут быть как меньше, так и больше, чем 300dip. Я использую Android 2.3.3, тестирование на устройстве HTC Evo 3D и эмуляторе (показывает одинаковый результат ).
Заранее большое спасибо.
UPD:
Спасибо Мигелю и Сэму. Решение состоит в том, чтобы установить maxHeight для textView, что увеличивает мой элемент списка (, который будет +id/thread_item_preview
), а также установить для RelativeLayout minHeight значение 300dip, чтобы предотвратить его сжатие.