gpt4 book ai didi

android - onMeasure 在 Android 6 的 Horizo​​ntalScrollView 中返回 widthMeasureSpec = 0

转载 作者:行者123 更新时间:2023-11-30 05:04:02 25 4
gpt4 key购买 nike

我相信我的问题与此有关:widthMeasureSpec is 0 when in HorizontalScrollView长话短说,我正在尝试创建自定义水平 ScrollView 。在某些时候,我压倒一切

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

虽然这在 Android >= 7 中工作正常,但我在 Android <= 6 中得到的 widthMeasureSpec 值为 0。measureChild 的代码在 API 23 和 24 之间发生了变化:

在 API 23 中:

childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

在 API 24 中:

final int childWidthMeasureSpec = MeasureSpec.makeSafeMeasureSpec(
Math.max(0, MeasureSpec.getSize(parentWidthMeasureSpec) - horizontalPadding),
MeasureSpec.UNSPECIFIED);

我尝试使用 getter 从父 View 获取宽度,但它没有按预期工作,因为只有一些调用获得了正确的宽度。我还尝试覆盖父级中的 measureChild 并将最新代码放在那里,但没有成功:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChild(getTouchInterceptTextView(), widthMeasureSpec, heightMeasureSpec);
}

@Override
protected void measureChild(View child, int parentWidthMeasureSpec,
int parentHeightMeasureSpec) {
ViewGroup.LayoutParams lp = child.getLayoutParams();

final int horizontalPadding = getPaddingLeft() + getPaddingRight();
final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
Math.max(0, MeasureSpec.getSize(parentWidthMeasureSpec) - horizontalPadding),
MeasureSpec.UNSPECIFIED);

final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
getPaddingTop() + getPaddingBottom(), lp.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}

我的全部文件在这里:https://github.com/AdrienPoupa/VinylMusicPlayer/commit/ba0dfae7dd03771f3625b8393927986aad552e2a

重要的部分是:

<com.poupa.vinylmusicplayer.views.TouchInterceptHorizontalScrollView
android:id="@+id/title_scrollview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<com.poupa.vinylmusicplayer.views.AutoTruncateTextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead" />

</com.poupa.vinylmusicplayer.views.TouchInterceptHorizontalScrollView>

onMeasure 函数在AutoTruncateTextView 中。我可以在 TouchInterceptHorizo​​ntalScrollView 中获得这些措施,但这似乎没有帮助。我尝试在 TouchInterceptHorizo​​ntalScrollView、AutoTruncateTextView 和两者的 XML 中添加 android:fillViewport="true"但它没有改变计算。

如何在 Android 6 及以下版本中获取正确的 widthMeasureSpec 值?

感谢您的帮助!

最佳答案

经过多次尝试,通过将以下代码 fragment 添加到 AutoTruncateTextView 的 onMeasure 似乎可以正常工作:

if (textBoundsWidth == 0) {
textBoundsWidth = getTouchInterceptFrameLayout().getMeasuredWidth();
}

基本上,获取 TouchInterceptFrameLayout 的宽度,即 XML 文件的第二层

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="72dp"
android:foreground="?attr/rectSelector"
tools:ignore="UnusedAttribute">

<!-- for getSwipeableContainerView() -->
<com.poupa.vinylmusicplayer.views.TouchInterceptFrameLayout
android:id="@+id/dummy_view"
android:layout_width="match_parent"
android:layout_height="match_parent">

并保留上面发布的代码,否则截断机制将不再起作用。

关于android - onMeasure 在 Android 6 的 Horizo​​ntalScrollView 中返回 widthMeasureSpec = 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54874431/

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