2

我有一个水平滚动视图,我应该了解这个滚动视图的宽度。我已经尝试了我在网上找到的所有解决方案,但结果都是一样的:0。滚动视图:

<HorizontalScrollView
    android:id="@+id/hsvMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@android:color/black"
    android:scrollbars="none" >

    <LinearLayout
        android:id="@+id/lyt_bultens"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="horizontal" >

        <TextView
            android:id="@+id/tvBultenMain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:lines="2"
            android:text="Bülten bulunamadı."
            android:textColor="@android:color/white"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:lines="2"
            android:text="Bülten bulunamadı."
            android:textColor="@android:color/white"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>
</HorizontalScrollView>

我已经尝试过:

toDeltaX = hsv.getChildAt(0).getMeasuredWidth();
toDeltaX = hsv.getChildAt(0).getRight();
toDeltaX = hsv.getChildAt(0).getWidth();
toDeltaX = hsv.getMeasuredWidth();
toDeltaX = hsv.getRight();
toDeltaX = hsv.getWidth();

int index = lytBultens.getChildCount() - 1;
View view = lytBultens.getChildAt(index);
toDeltaX = view.getRight();

请帮忙。

4

2 回答 2

6

oncreate 方法中的 Layouts 将高度和宽度返回为零。您可以添加获取高度和宽度的方法。

      @Override
   public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
    //Here you can get the size!

}
于 2013-07-24T13:14:04.347 回答
0

这很简单:试试下面的代码:

ScrollView myScrollView = (ScrollView) findViewById(R.id.hsvMain);
myScrollView = (FrameLayout.LayoutParams) myScrollView .getLayoutParams();
Float width = myScrollView .width;

干杯!

于 2013-07-24T11:14:56.683 回答