Android 制作水平列表视图';s列表项';s高度动态

Android 制作水平列表视图';s列表项';s高度动态,android,android-layout,listview,android-listview,Android,Android Layout,Listview,Android Listview,我在页面底部有一个按钮列表,在水平列表视图中,我只想在列表视图中显示4行,当屏幕变大时,它的项目大小也应该变大,所以只有4个项目显示 比如说 在这里您可以看到,在增加宽度大小之后,它的相关水平listview的listitem也在增加 可能吗?如果可能的话,任何人都能建议我如何处理它吗?也许可以尝试获得屏幕宽度,然后将列表的元素设置为该宽度的1/4 类似于onResume/onCreate Display display = getWindowManager().getDefaultDispl

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

比如说

在这里您可以看到,在增加宽度大小之后,它的相关水平listview的listitem也在增加


可能吗?如果可能的话,任何人都能建议我如何处理它吗?

也许可以尝试获得屏幕宽度,然后将列表的元素设置为该宽度的1/4

类似于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)

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

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>
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;
}
给你,它会很好用的


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

是的,它是。但是您需要先共享一些代码。在Activity或Fragment的
onCreate
中,获取以像素为单位的窗口宽度并除以4。在适配器的
getView()
中,将此数字用作返回视图的布局参数中的宽度。
@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;
}