[英]How can I make my listview rows different heights
Right now if there is a list item that is 2 lines in height then all the rows are two lines in height 现在,如果列表项的高度为2行,则所有行的高度均为2行
How can I make the rows' height independent on other rows. 如何使行的高度独立于其他行。
ListView 列表显示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/user_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null" />
</LinearLayout>
Row 行
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="1dp"
android:paddingTop="1dp"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:maxLines="2"
android:ellipsize="middle"
android:textColor="@android:color/holo_blue_dark"
android:textStyle="bold" />
<TextView
android:id="@+id/points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:gravity="right"
android:textStyle="bold" />
</LinearLayout>
I am using a custom adapter extending BaseAdapter. 我正在使用扩展BaseAdapter的自定义适配器。 the getView there is nothing special, just a viewholder patern I don't change height or anything
getView没有什么特别的,只是一个viewholder模式,我不会改变高度或其他任何东西
why don't you try making the height = 0dp and weight 1 为什么不尝试使身高= 0dp和体重1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="1dp"
android:paddingTop="1dp"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:maxLines="2"
android:ellipsize="middle"
android:textColor="@android:color/holo_blue_dark"
android:textStyle="bold" />
<TextView
android:id="@+id/points"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="right"
android:layout_weight="1"
android:gravity="right"
android:textStyle="bold" />
</LinearLayout>
update: 更新:
check this answer 检查这个答案
https://stackoverflow.com/a/7513423/1932105 https://stackoverflow.com/a/7513423/1932105
You are restricting it the TextView
to have a maximum of 2 lines. 您将
TextView
限制为最多2行。 It will never grow more than that. 它永远不会比现在增长更多。 Remove
android:maxLines="2"
. 删除
android:maxLines="2"
。 It should work automatically. 它应该自动工作。
The trick for me was not setting the height -- but instead setting the minHeight.
对我来说,诀窍不是设置高度-而是设置minHeight。 This must be applied to the root view of whatever layout your custom adapter is using to render each row.
必须将其应用于自定义适配器用来呈现每一行的任何布局的根视图。
Source: https://stackoverflow.com/a/7368439/2498729 资料来源: https : //stackoverflow.com/a/7368439/2498729
android:textAppearance="?android:attr/textAppearanceLarge"
seemed no effect.
似乎没有效果。
android:minHeight="?android:attr/listPreferredItemHeight"
changed the height for me
为我改变了身高
Source: https://stackoverflow.com/a/4274505/2498729 资料来源: https : //stackoverflow.com/a/4274505/2498729
Looks like you'll have to make visibility of your text views to be View.GONE
when they are empty. 看来您必须在文本视图为空时使它们的可见性变为
View.GONE
。 You can do it in adapter implementation. 您可以在适配器实现中做到这一点。
pointsView.setText(pointsText);
pointsView.setVisibility(TextUtils.isEmpty(pointsText) ? View.GONE : View.VISIBLE);
But be careful since your views are in RelativeLayout
and its rules can stop working when an anchored view gets visibility View.GONE
. 但是要小心,因为您的视图位于
RelativeLayout
并且当锚定视图获得可见性View.GONE
时,其规则可能会停止工作。
In such case you may try setting text size to zero instead of changing the view visibility, but I haven't tried it myself. 在这种情况下,您可以尝试将文本大小设置为零,而不是更改视图的可见性,但是我自己还没有尝试过。
pointsView.setTextSize(TextUtils.isEmpty(pointsText) ? 0 : defaultSize);
If you are not limited with the TextView MaxLine then please remove android:maxLines="2"
this will automatically adjust the height of list. 如果您不受TextView MaxLine的限制,请删除
android:maxLines="2"
这将自动调整列表的高度。
If you are looking for increasing the height of one particular row independent of others. 如果您正在寻找增加特定行的高度而又不增加其他行的高度。 After setting the adapter value for the list.
在为列表设置适配器值之后。 Get the list
取得清单
final ListView lv = getListView();
then form the lv
get the particular row by 然后形成
lv
获取特定行
lv.getItemIdAtPosition(position)
or any similar logic that best suited in your code. lv.getItemIdAtPosition(position)
或最适合您的代码的任何类似逻辑。 and change the row appearance programmatically as you looking for. 并根据需要以编程方式更改行的外观。 Thanks.
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.