簡體   English   中英

如何以編程方式獲取帶有按鈕的視圖的高度?

[英]how to programmatically get the height of a view with buttons on it?

好的,這是我的問題我想創建一個滾動視圖,填充到屏幕底部的一組兩個按鈕。 知道我不能只設置滾動視圖來填充父級我以為我會去獲得線性布局的高度,其中包含按鈕,然后是我設置為填充父級的滾動視圖的高度。 然后從另一個中減去一個,然后重置滾動視圖高度。 我想出了如何獲取和設置滾動視圖高度。 我找到了一種方法可以測量活動中的所有其他視圖,但是這個視圖總是返回零,我不知道如何獲得它的高度。

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>

如果小部件沒有出現在屏幕上,他們就不會自己想出來了。 因此按鈕元素仍處於零,因為它們從未呈現過; 並且它們從未呈現過,因為滾動視圖將它們推離屏幕。 我這次從另一個角度攻擊了這個問題我所做的就是將滾動視圖設置為一個任意的小數字,這樣按鈕就停留在屏幕上,將頂層布局的大小存儲到“int totalsize”,然后我得到除了滾動視圖之外的所有元素的大小,並獲得每個視圖的邊距,並將它們全部合計為“int used”,然后我將滾動視圖高度設置為“totalsize-used”,這就是有效的。 發現它我手動累計邊距不起作用,當屏幕尺寸改變時,邊距也會發生變化,因此發現它們以及視圖尺寸效果最佳。

在我的on create方法中我得到了這個:

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);
}

希望這有助於某人也同樣的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM