2013-07-24 97 views
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

oncreate方法中的佈局將高度和寬度返回爲零。您可以添加一個獲取高度和寬度的方法。

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

} 
0

這是非常簡單的: 試試下面的代碼:

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

乾杯!

+0

我用android:layout_width =「match_parent」,所以它的工作? – ismailarilik