[英]obtain width of HorizontalScrollView child
我正在嘗試獲取 HorizontalScrollView 子項的寬度,但是我的寬度始終與我的 HorizontalScrollView 相同
我嘗試覆蓋 onMeasure 和 onLayout 並調用 measure() 但我得到 0、100 或與我的 HorizontalScrollView 相同的寬度
我目前的代碼是
void adjustClip(int r, int b) {
// ommited
}
void getWindowSize() {
if (mOrientation == VERTICAL) {
windowHeight = document.getHeight();
} else {
windowWidth = document.getWidth();
}
}
boolean getDocumentSizeTypeRecyclerView() {
// ommited
}
void printWidths(View view) {
Log.d(TAG, "view.getRight() = [" + (view.getRight()) + "]");
Log.d(TAG, "view.getWidth() = [" + (view.getWidth()) + "]");
Log.d(TAG, "view.getMeasuredWidth() = [" + (view.getMeasuredWidth()) + "]");
Log.d(TAG, "view.getMeasuredWidthAndState() = [" + (view.getMeasuredWidthAndState()) + "]");
Log.d(TAG, "view.getMinimumWidth() = [" + (view.getMinimumWidth()) + "]");
Log.d(TAG, "view.getVerticalScrollbarWidth() = [" + (view.getVerticalScrollbarWidth()) + "]");
}
boolean getDocumentSizeTypeHorizontalScrollView() {
if (document instanceof HorizontalScrollView) {
HorizontalScrollView horizontalScrollView = (HorizontalScrollView) document;
printWidths(horizontalScrollView);
printWidths(horizontalScrollView.getChildAt(0));
View child = ((ViewGroup) document).getChildAt(0);
documentWidth = child.getWidth();
return true;
} else return false;
}
boolean getDocumentSize() {
if (getDocumentSizeTypeRecyclerView()) return true;
if (getDocumentSizeTypeHorizontalScrollView()) return true;
return false;
}
void setThumbSize(int b, int r) {
if (mOrientation == VERTICAL) {
documentHeightDivWindowHeight = documentHeight / windowHeight;
float thumbHeight = b / documentHeightDivWindowHeight;
clip.setHeight((int) thumbHeight);
} else {
documentWidthDivWindowWidth = documentWidth / windowWidth;
float thumbWidth = r / documentWidthDivWindowWidth;
clip.setWidth((int) thumbWidth);
}
}
void doScroll(int r, int b) {
if (document != null) {
getWindowSize();
if (getDocumentSize()) {
setThumbSize(b, r);
if (!scrolling) scrollDocument();
}
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (DEBUG) Log.d(TAG, "onLayout() called with: changed = [" + changed + "], l = [" + l + "], t = [" + t + "], r = [" + r + "], b = [" + b + "]");
if (layout) {
layout = false;
} else {
layout = true;
adjustClip(r, b);
doScroll(r, b);
}
}
這就是它打印的內容
D/ScrollBarView: onLayout() called with: changed = [false], l = [80], t = [0], r = [760], b = [80]
// HorizontalScrollView
view.getRight() = [1140]
view.getWidth() = [840]
view.getMeasuredWidth() = [840]
view.getMeasuredWidthAndState() = [840]
view.getMinimumWidth() = [0]
view.getVerticalScrollbarWidth() = [14]
// child
view.getRight() = [840]
view.getWidth() = [840]
view.getMeasuredWidth() = [840]
view.getMeasuredWidthAndState() = [840]
view.getMinimumWidth() = [0]
view.getVerticalScrollbarWidth() = [0]
因為我正在嘗試為 android 制作外部滾動條視圖,因為我無法修改 ScrollView 和相關的內部滾動條
如果我理解正確,您在計算並放置在屏幕上之前測量的視圖大小。
我認為您可以添加一個偵聽器,該偵聽器將在計算完布局后運行
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
printWidths(view);
}
});
波紋管如何在代碼中使用它:
主要活動:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ExternalScrollBar esb = (ExternalScrollBar) findViewById(R.id.esb);
esb.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
esb.getViewTreeObserver().removeOnGlobalLayoutListener(this);
LinearLayout ll = (LinearLayout) esb.getChildAt(0);
TextView tv = (TextView) ll.getChildAt(0);
TextView tv2 = (TextView) ll.getChildAt(1);
printWidths(esb);
printWidths(tv);
printWidths(tv2);
}
});
}
void printWidths(View view) {
System.out.println("view.getRight() = [" + (view.getRight()) + "]");
System.out.println("view.getWidth() = [" + (view.getWidth()) + "]");
System.out.println("view.getMeasuredWidth() = [" + (view.getMeasuredWidth()) + "]");
System.out.println("view.getMeasuredWidthAndState() = [" + (view.getMeasuredWidthAndState()) + "]");
System.out.println("view.getMinimumWidth() = [" + (view.getMinimumWidth()) + "]");
System.out.println("view.getVerticalScrollbarWidth() = [" + (view.getVerticalScrollbarWidth()) + "]");
}
}
外部滾動條
public class ExternalScrollBar extends HorizontalScrollView {
public ExternalScrollBar(Context context) {
super(context);
}
public ExternalScrollBar(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
System.out.println(this.getWidth());
System.out.println(((LinearLayout)this.getChildAt(0)).getChildAt(0).getWidth());
System.out.println(((LinearLayout)this.getChildAt(0)).getChildAt(1).getWidth());
}
}
活動布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
tools:context=".MainActivity">
<com.example.stackhorizontalchild.ExternalScrollBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/esb"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World 5212! "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Wor!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</com.example.stackhorizontalchild.ExternalScrollBar>
</LinearLayout>
運行結果:
I/System.out: 472 //onLayout
I/System.out: 302
I/System.out: 170
view.getRight() = [472] //ExternalScrollBar
view.getWidth() = [472]
view.getMeasuredWidth() = [472]
view.getMeasuredWidthAndState() = [472]
view.getMinimumWidth() = [0]
view.getVerticalScrollbarWidth() = [11]
view.getRight() = [302] //Child TextView1
view.getWidth() = [302]
view.getMeasuredWidth() = [302]
view.getMeasuredWidthAndState() = [302]
view.getMinimumWidth() = [0]
view.getVerticalScrollbarWidth() = [0]
view.getRight() = [472] //Child TextView2
view.getWidth() = [170]
view.getMeasuredWidth() = [170]
view.getMeasuredWidthAndState() = [170]
view.getMinimumWidth() = [0]
view.getVerticalScrollbarWidth() = [0]
這好像是
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
View child = horizontalScrollView.getChildAt(0);
if (child == null) {
width = child.getWidth();
}
}
}
是獲取水平滾動視圖寬度的正確方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.