将Android ListView行高设置为最大行

时间:2022-08-24 07:35:23

I have a ListView displaying some text on my android activity. The problem I am facing is that some text is bigger than the other so the ListView rows don't have the same size. I would like to have all the rows the size of the biggest. That way all the text will be display and they will all have the same size. Here is my xml :

我有一个ListView在我的android活动上显示一些文本。我面临的问题是一些文本比另一个文本大,所以ListView行的大小不同。我希望所有的行都是最大的行。这样,所有文本都将显示,并且它们都具有相同的大小。这是我的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <CheckBox
            android:id="@+id/favorite_checkbox"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            style="?android:attr/starStyle"
            android:layout_weight="1"
            />
    <TextView
        android:id="@+id/IdText"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        />

    <TextView
        android:id="@+id/StationNameText"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        />

    </LinearLayout>

</LinearLayout>

Thanks for your time.

谢谢你的时间。

1 个解决方案

#1


0  

You can get the height of the 'tallest' view programmatically - How to get an Android widget's size after layout is calculated?, and then set that height to your other layouts.

您可以通过编程方式获得“最高”视图的高度 - 如何在计算布局后获取Android小部件的大小?然后将该高度设置为其他布局。

I would do something like this:

我会做这样的事情:

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int width = displaymetrics.widthPixels;

int HeightOfTallestView = //method you choose from link above

IdText.setLayoutParams(new LinearLayout.LayoutParams(width, HeightOfTallestView ));

Please let me know if this works for you.

如果这对你有用,请告诉我。

#1


0  

You can get the height of the 'tallest' view programmatically - How to get an Android widget's size after layout is calculated?, and then set that height to your other layouts.

您可以通过编程方式获得“最高”视图的高度 - 如何在计算布局后获取Android小部件的大小?然后将该高度设置为其他布局。

I would do something like this:

我会做这样的事情:

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int width = displaymetrics.widthPixels;

int HeightOfTallestView = //method you choose from link above

IdText.setLayoutParams(new LinearLayout.LayoutParams(width, HeightOfTallestView ));

Please let me know if this works for you.

如果这对你有用,请告诉我。