如何在基于权重的布局中创建与列表项一样大的列表视图?编程?

时间:2014-12-19 12:50:51

标签: android xml listview resize

我有一个布局,它应该有一个可滚动的顶部列表视图,而listitem应该是布局本身的大小!但布局是用权重定义的...它应该是这样的:

what i want to achieve!

但是只要我用listitems添加适配器,视图就会调整大小!并推动其他权重集(图像)视图更小! 我可以将listitem(这是一个单独的片段)设置为动态大小(如100dp),但是在不同的分辨率下,它不会像它应该适合的布局那么大。

my menulayout:

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="4">
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/startmenu_lv_spotlight"
        android:listSelector="@drawable/selector_view" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:weightSum="2">
        <ImageView
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_width="wrap_content"
            android:id="@+id/startmenu_iv_shops"
            android:layout_weight="1"
            android:scaleType="centerCrop"
            android:layout_height="fill_parent" />
        <ImageView
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:id="@+id/startmenu_iv_categories"
            android:scaleType="centerCrop"
            android:layout_height="fill_parent" />
    </LinearLayout>
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:id="@+id/startmenu_iv_review"
        android:scaleType="centerCrop"
        android:layout_height="0dp" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:weightSum="2">
        <ImageView
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:scaleType="centerCrop"
            android:id="@+id/startmenu_iv_newmovie"
            android:layout_height="fill_parent" />
        <ImageView
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:scaleType="centerCrop"
            android:id="@+id/startmenu_iv_personalactions"
            android:layout_height="fill_parent" />
    </LinearLayout>
</LinearLayout>

和我的列表项片段:

<?xml version="1.0" encoding="utf-8"?>
alright in the fragment of the listITEM i've changed to:

schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:weightSum="4">
    <ImageView
        android:id="@+id/deal_li_iv_icon_category"
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="0dp"
        android:layout_height="120dp"
        android:layout_weight="1" />
    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/deal_li_tv_text"
            android:layout_toRightOf="@id/deal_li_iv_icon_category"
            android:text="text here"
            android:paddingTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
</LinearLayout>

我应该如何创建它,以便在设置适配器时,ListView不会调整其边界大小!和listitems将与listview的边界一样大吗?

1 个答案:

答案 0 :(得分:0)

您可以将高度传递给列表适配器。 在适配器的getView方法中,您可以强制布局容器的高度。

首先在布局容器中添加一个ID:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lineLayout"
    schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:weightSum="4">
    <ImageView
        android:id="@+id/deal_li_iv_icon_category"
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="0dp"
        android:layout_height="120dp"
        android:layout_weight="1" />
    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/deal_li_tv_text"
            android:layout_toRightOf="@id/deal_li_iv_icon_category"
            android:text="text here"
            android:paddingTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
</LinearLayout>

适配器的getView方法应如下所示:

@Override
public View getView(int index, View rowView, ViewGroup viewGroup) {

    LayoutInflater inflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    LinearLayout lineLayout = (LinearLayout)inflater.inflate(R.layout.inflate_friend_line_adapter, (ViewGroup)rowView.findViewById(R.id.lineLayout));
    AbsListView.LayoutParams param = new AbsListView.LayoutParams(_width, _height);
    lineLayout.setLayoutParams(param);

    //example
    TextView text = (TextView) lineLayout.findViewById(R.id.text);

    return lineLayout;
}