2012-01-22 25 views
4

好的這是我的問題我想創建一個滾動視圖,填充到屏幕底部的兩個按鈕集合。知道我不能只設置滾動視圖來填充父項我以爲我會去獲取線性佈局的高度與它中的按鈕,然後我設置填充父級的滾動視圖的高度。然後從另一箇中減去一個,然後重置滾動視圖高度。我想出瞭如何獲取和設置滾動視圖高度。我發現了一種方法來測量活動中的所有其他視圖,但是使用這個視圖時,它總是返回一個零,並且我不知道如何獲得它的高度。如何以編程方式獲取按鈕上的視圖的高度?

final LinearLayout tv = (LinearLayout)findViewById(R.id.thebuttons); 
    ViewTreeObserver vto = tv.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
     public void onGlobalLayout() { 
      Integer grr = tv.getHeight(); 
      Log.e("the h", grr.toString()); 
      ViewTreeObserver obs = tv.getViewTreeObserver(); 
      obs.removeGlobalOnLayoutListener(this); 
     } 
    }); 

現在如果我將它設置爲任何我的其他佈局中,我得到了它的高度,我希望,但將其設置爲這一個,我得到0;這是我正在測量的那個!我也試過直接測量按鈕無濟於事

<LinearLayout 
android:id="@+id/thebuttons" 
android:orientation="horizontal" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center_horizontal" 
android:layout_marginTop="5sp" 
> 
<Button 
    android:id="@+id/saveit" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Save" /> 
<Button 
    android:id="@+id/clearit" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Save" /> 
</LinearLayout> 
+0

只需使用一個[ 'RelativeLayout'](http://developer.android.com/resources/tutorials/views/hello-relativelayout.html)而不是'LinearLayout',這使您可以輕鬆地按照您描述的方式定位元素而無需執行任何討厭的測量等 – tomtheguvnor

回答

12

我終於想通了自己 如果部件沒有在屏幕上他們沒有得到創建出現。所以因此按鈕元素仍然在零處,因爲它們從未被渲染;他們從來沒有渲染,因爲滾動視圖將它們推離屏幕。 所以我從一個不同的角度攻擊了這個問題,這次我將滾動視圖設置爲一個任意的小數字,以便按鈕保持在屏幕上,將頂層佈局的大小存儲爲「int totalsize」,然後我獲取除滾動視圖以外的所有元素的大小,並獲得每個視圖的邊距,並將它們總計爲「int used」,然後將滾動視圖高度設置爲「totalsize-used」,這就是工作原理。發現它我手動收集邊距,當屏幕尺寸發生變化時邊緣不起作用,邊距也發生變化,因此發現它們與視圖尺寸一起效果最好。

在我的

創建方法,我得到這個:

final Button tv = (Button)findViewById(R.id.saveit); 
    ViewTreeObserver vto = tv.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
     public void onGlobalLayout() { 
      ViewGroup.MarginLayoutParams vlp = (MarginLayoutParams) tv.getLayoutParams(); 
      int btnsize =tv.getMeasuredHeight()+vlp.topMargin; 
      sizeit(btnsize); 
      ViewTreeObserver obs = tv.getViewTreeObserver(); 
      obs.removeGlobalOnLayoutListener(this); 
     } 
    }); 

,然後它的大小功能

private void sizeit(Integer thebuttons) 
{ 
    View v = findViewById(R.id.viewmain); 
    int total = v.getHeight(); 
    v = findViewById(R.id.view1); 
    ViewGroup.MarginLayoutParams vlp = (MarginLayoutParams) v.getLayoutParams(); 
    int used=v.getHeight()+vlp.topMargin; 
    v = findViewById(R.id.view2); 
    vlp = (MarginLayoutParams) v.getLayoutParams(); 
    used+=v.getHeight()+vlp.topMargin; 
    v = findViewById(R.id.fonttext); 
    vlp = (MarginLayoutParams) v.getLayoutParams(); 
    used+=v.getHeight()+vlp.topMargin; 
    v = findViewById(R.id.infolabel); 
    vlp = (MarginLayoutParams) v.getLayoutParams(); 
    used+=v.getHeight()+vlp.topMargin; 
    Integer scrsize=total-used-thebuttons; 
    v = findViewById(R.id.scrview); 

    ScrollView scr = (ScrollView)findViewById(R.id.scrview); 
    ViewGroup.LayoutParams scrlprams = scr.getLayoutParams(); 
    scrlprams.height=scrsize; 
    scr.setLayoutParams(scrlprams); 
} 

希望這可以幫助別人也有同樣的問題戰鬥

相關問題