gpt4 book ai didi

Android在Adapter的GetView方法中获取ListView的宽度

转载 作者:行者123 更新时间:2023-11-30 02:13:21 26 4
gpt4 key购买 nike

如何在 Adapter 的 GetView 方法中获取 ListView 的宽度?

View 的 ParentLayout 是一个宽度为 match_parentRelativeLayout

以下是我尝试过的

Display display = ((Activity)getContext()).getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
maxWidth = size.x;
int specWidth = View.MeasureSpec.makeMeasureSpec(maxWidth , View.MeasureSpec.AT_MOST);
convertView.findViewById(R.id.TagMasterLayout).measure(specWidth , specWidth);
int totalWidth = convertView.findViewById(R.id.TagMasterLayout).getMeasuredWidth();

在这个例子中,我总是得到设备屏幕的宽度。因为我已将 MeasureSpec 设置为接受设备屏幕宽度的最大值。

相反,如果我这样做,

int specWidth = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

其余所有部分保持不变,我得到一个大于 1000 的巨大值(大于设备屏幕宽度,因为变量 maxWidth 的值仅为 720)。

那么如何在 Adapter 类的 getView 方法中精确测量宽度为 fill_parentListView 的宽度?

最佳答案

尝试获取父 View 的宽度。这是您的 View 将附加到的 View 。

@Override
public View getView(int position, View convertView, final ViewGroup parent)
{
...

parent.getWidth();

...
return v;
}

parent.getWidth() 将返回 ListView 的宽度。

关于Android在Adapter的GetView方法中获取ListView的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29796577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com