如何获得水平滚动视图的宽度

时间:2013-07-24 11:08:07

标签: android width horizontalscrollview

我有一个水平滚动视图,我应该学习这个滚动视图的宽度。我尝试过在web中找到的所有解决方案,但结果是一样的: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();

请帮忙。

2 个答案:

答案 0 :(得分:5)

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

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

}

答案 1 :(得分:0)

很简单: 尝试以下代码:

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

干杯!