2014-06-17 49 views
0

我想建立一个应用程序,其中有许多项目的列表视图,但我无法更改或设置单个项目的宽度和高度。我到处搜索,我得到的答案是使宽度FILL_PARENT,但它不是为我工作...如何更改android中listviews项的宽度和高度?

请帮助....在此先感谢...这里是代码:

<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> 
+0

其中是您的列表适配器视图(你想添加到你的列表视图)?发布xml代码和您的适配器类 – k0sh

+0

使用自定义视图。 – Aniruddha

回答

0

This link向您展示了如何使用自己的自定义适配器在java中执行此操作。

当在适配器中重写getView()时,您可以在将视图提供给渲染框架之前修改高度。另外,请注意,您不必使用SimpleCursorAdapter,也可以以相同的方式使用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; 
    } 
} 
1

如果要动态改变列表视图的高度,你可以使用

list.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant)); 

import android.view.ViewGroup.LayoutParams; 
ListView mListView = (ListView) findViewById(R.id.listviewid); 
LayoutParams list = (LayoutParams) mListView.getLayoutParams(); 
list.height = set the height acc to you;//like int 200 
mListView.setLayoutParams(list); 
相关问题