机器人 - 获取ListView项的高度?机器人、高度、ListView

2023-09-04 23:00:24 作者:念你的时光比爱长

有没有办法让ListViewItem的高度code,当在列表中没有实际的项目?

Is there a way to get ListViewItem height in code, when there is no actual items in list?

我的ListViewItem的布局:

My ListViewItem layout:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight">
...
</LinearLayout>  

我试图把它用吹气:

I have tried to get it using Inflater:

View convertView = LayoutInflater.from( this )
    .inflate( R.layout.mail_list_row, null );
int itemHeight = convertView.getHeight();

但它返回0;

But it's return 0;

谢谢!

推荐答案

在Android的看法是分配的宽度和高度,只有当它的渲染完成。所以,除非你列表ATLEAST渲染一旦你不会得到listItemHeight。解决你的问题可能是你设置一些最低高度列表项的,让你有ATLEAST东西,而不是硬编码的高度和宽度工作。

In android a view is assigned Width and Height only when its rendering is complete. So unless you list is rendered atleast once you won't get listItemHeight. Solution to your problem could be that you set some min Height of list Item so that you have atleast something to work with instead of Hard Coding height and width.