【问题标题】:How can i create a listview as big as it's listitems in a layout based on weights? programmaticaly?如何根据权重在布局中创建与列表项一样大的列表视图?以编程方式?
【发布时间】:2014-12-19 12:50:51
【问题描述】:

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

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

我的菜单布局:

<?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 不会调整其边界!并且列表项将与列表视图边界的高度一样大?

【问题讨论】:

  • 将那些水平LinearLayouts中ImageViews的layout_width设置为0dp,而不是wrap_content
  • 但问题是,我似乎无法弄清楚如何在列表视图中设置列表项的高度!到 listview.heigh 的大小...!
  • 你有 wrap_content 作为高度。匹配/填充父级可能是您想要的更多

标签: android xml listview resize


【解决方案1】:

您可以将高度传递给列表适配器。 在您的适配器的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;
}

【讨论】:

  • 如何通过 getview 强制高度?不是列表视图的布局,而是列表项的布局! :) 不确定你是不是这个意思
猜你喜欢
  • 1970-01-01
  • 2013-03-20
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
  • 2015-05-03
  • 1970-01-01
  • 2019-03-12
  • 1970-01-01
相关资源
最近更新 更多