如何在android中更改ListView项目的宽度和高度?
我正在尝试构建一个应用程序,其中有一个包含许多项的列表视图,但我无法更改或设置单个项的宽度和高度。我到处搜索,得到的答案是将宽度填充为父项,但这对我不起作用 请帮忙。。。。先谢谢你。。。以下是代码:如何在android中更改ListView项目的宽度和高度?,android,android-intent,android-activity,Android,Android Intent,Android Activity,我正在尝试构建一个应用程序,其中有一个包含许多项的列表视图,但我无法更改或设置单个项的宽度和高度。我到处搜索,得到的答案是将宽度填充为父项,但这对我不起作用 请帮忙。。。。先谢谢你。。。以下是代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_wi
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
tools:context=".CustomListViewAndroidExample" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</RelativeLayout>
如果要动态更改列表视图的高度,可以使用
list.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));
或
向您展示了如何在java中使用您自己的自定义适配器进行此操作
在适配器中重写getView()时,可以在将视图提供给框架进行渲染之前修改高度。另外,请注意,您不必使用SimpleCorsorAdapter,ArrayAdapter也可以以相同的方式使用
final SimpleCursorAdapter adapter = new SimpleCursorAdapter (context, cursor) {
@Override
public View getView (int position, View convertView, ViewGroup parent) {
final View view = super.getView(position, convertView, parent);
final TextView text = (TextView) view.findViewById(R.id.tvRow);
final LayoutParams params = text.getLayoutParams();
if (params != null) {
params.height = mRowHeight;
}
return view;
}
}
列表适配器视图(要添加到listview的视图)在哪里?发布xml代码和适配器类
final SimpleCursorAdapter adapter = new SimpleCursorAdapter (context, cursor) {
@Override
public View getView (int position, View convertView, ViewGroup parent) {
final View view = super.getView(position, convertView, parent);
final TextView text = (TextView) view.findViewById(R.id.tvRow);
final LayoutParams params = text.getLayoutParams();
if (params != null) {
params.height = mRowHeight;
}
return view;
}
}