gpt4 book ai didi

android - 为什么 getLocationOnScreen(location) 总是返回 0?

转载 作者:搜寻专家 更新时间:2023-11-01 08:34:40 28 4
gpt4 key购买 nike

在我的 FragmentLayout 中,我有一个带有多个 subview (TextViewCardView)的 LinearLayout。我想找到所有 LinearLayout View 的顶部偏移量,但我总是得到零。
这是我的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_detail, container, false);

nestedScrollView = (NestedScrollView) view.findViewById(R.id.nestedScrollView);
linearLayout = (LinearLayout) view.findViewById(R.id.lll);

int childCount = linearLayout.getChildCount();
int[] location = new int[2];
for (int i = 0; i < childCount; i++) {
View v = linearLayout.getChildAt(i);
v.getLocationOnScreen(location);

Log.e("locX", location[0] + "");
Log.e("locY", location[1] + "");

}


return view;
}

这是我的布局 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.AppBarLayout>
<android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<LinearLayout android:id="@+id/lll">

// views are there

</LinearLayout>


</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>

最佳答案

onWindowFocusChanged(boolean hasFocus) 方法中编写查找位置的函数,或者将任务发布到具有 100 毫秒延迟的处理程序上。

关于android - 为什么 getLocationOnScreen(location) 总是返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37790355/

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