- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我相信我的问题与此有关: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 中。我可以在 TouchInterceptHorizontalScrollView 中获得这些措施,但这似乎没有帮助。我尝试在 TouchInterceptHorizontalScrollView、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 的 HorizontalScrollView 中返回 widthMeasureSpec = 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54874431/
谁能告诉我参数的单位是什么 onMeasure(int widthMeasureSpec, int heightMeasureSpec) 这个 View 类方法。 我打印日志,它分别返回 107374
我相信我的问题与此有关:widthMeasureSpec is 0 when in HorizontalScrollView长话短说,我正在尝试创建自定义水平 ScrollView 。在某些时候,我压
我是一名优秀的程序员,十分优秀!