使水平列表视图listitem高度动态

时间:2013-12-13 14:07:20

标签: android android-layout listview android-listview

我在页面底部有按钮列表,在水平列表视图中,我想在列表视图中只显示4行,当屏幕变大时,它的项目大小也应该变大,所以只有4项显示。

例如

enter image description here

在这里你可以看到,在增加宽度大小后,它的相关水平列表视图的listitem也在增加

有可能吗?如果可能,任何机构都可以建议我如何处理它?<​​/ p>

2 个答案:

答案 0 :(得分:1)

也许尝试获取屏幕宽度,然后将List的元素设置为该宽度的1/4?

on onResume / onCreate

之类的东西
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x; //remember that width

在getView中

LayoutParams params=new LayoutParams(width/4, -1);
element.setLayoutParams(params)

答案 1 :(得分:1)

创建一个自定义布局类,如下所示..

public class MyLinearLayout extends LinearLayout {

public MyLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyLinearLayout(Context context) {
    super(context);
}

public void setLayoutParams(int width) {
    LayoutParams params = new LayoutParams(width / 4,
            LayoutParams.WRAP_CONTENT);
    this.setLayoutParams(params);
}

@Override
public void setLayoutParams(android.view.ViewGroup.LayoutParams params) {
    super.setLayoutParams(params);
}

}

将此水平滚动视图放在xml文件中

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <com.example.widget.MyLinearLayout
            android:id="@+id/llMainHome"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/orange_bg"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/home" />
        </com.example.widget.MyLinearLayout>

        <com.example.widget.MyLinearLayout
            android:id="@+id/llHomeSearch"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/green_bg"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/search" />
        </com.example.widget.MyLinearLayout>

        <com.example.widget.MyLinearLayout
            android:id="@+id/llHomeFavorite"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/green_bg"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/favorite" />
        </com.example.widget.MyLinearLayout>

        <com.example.widget.MyLinearLayout
            android:id="@+id/llHomeGrocery"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/green_bg"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/make_juice" />
        </com.example.widget.MyLinearLayout>

        <com.example.widget.MyLinearLayout
            android:id="@+id/llHomeSettings"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/green_bg"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/settings" />
        </com.example.widget.MyLinearLayout>
    </LinearLayout>
</HorizontalScrollView>

并在您的活动类文件中声明

private MyLinearLayout llHomeSearch, llHomeFavorite,
        llHomeGrocery, llHomeSettings,llMainHome;

oncreate方法代码是

llHomeFavorite = (MyLinearLayout) findViewById(R.id.llHomeFavorite);
    llHomeFavorite.setOnClickListener(this);

    llMainHome = (MyLinearLayout) findViewById(R.id.llMainHome);
    llMainHome.setOnClickListener(this);

    llHomeSearch = (MyLinearLayout) findViewById(R.id.llHomeSearch);
    llHomeSearch.setOnClickListener(this);

    llHomeGrocery = (MyLinearLayout) findViewById(R.id.llHomeGrocery);
    llHomeGrocery.setOnClickListener(this);

    llHomeSettings = (MyLinearLayout) findViewById(R.id.llHomeSettings);
    llHomeSettings.setOnClickListener(this);
    Display display = getWindowManager().getDefaultDisplay();
    Point size = getDisplaySize(display);
    int width = size.x;
    llMainHome.setLayoutParams(width);
    llHomeFavorite.setLayoutParams(width);
    llHomeGrocery.setLayoutParams(width);
    llHomeSearch.setLayoutParams(width);
    llHomeSettings.setLayoutParams(width);

和getDisplaysize方法是

@SuppressLint("NewApi")
private static Point getDisplaySize(final Display display) {
    final Point point = new Point();
    try {
        display.getSize(point);
    } catch (java.lang.NoSuchMethodError ignore) { // Older device
        point.x = display.getWidth();
        point.y = display.getHeight();
    }
    return point;
}

在这里你去..它会很好..

如果您的标签栏中有固定数量的项目,则无需采用水平列表视图。