get the height of layout which has wrap_content property

老子叫甜甜 提交于 2020-01-25 23:45:06

问题


I have a simple table and a scrollView for scrolling the TableRow. I had also added footer the problem is that i need to know what is the size of footer so I can give the android:layout_marginBottom="<size of footerbar> to my scrollView. So how can i get the height of the layout of footer bar? please help me.

this is my footerbar code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/btnColor"
android:orientation="horizontal" >


<Button
    android:id="@+id/previous"
    style="@style/pagingBarBtnAttr"
    android:layout_alignParentLeft="true"
    android:background="@drawable/choose_color_btn"
    android:drawableLeft="@drawable/previous"
    android:text="Previous" />

<TextView
    android:id="@+id/pageNo"
    style="@style/pagingBarBtnAttr"
    android:layout_centerHorizontal="true"
    android:text="Page No. 1" />

<Button
    android:id="@+id/next"
    style="@style/pagingBarBtnAttr"
    android:layout_alignParentRight="true"
    android:background="@drawable/choose_color_btn"
    android:drawableRight="@drawable/next"
    android:text="Next" />

now as the next and previous button has a drawable so they have the drawable according to device size so the footer bar has no fixed value of its height so how can i get its height so i can supply that value to margin_bottom of scrollview. Thanks in advance..


回答1:


Refer the below code

First add footer in your xml file and set its alignParentBottom to true.

Then add scrollView and set its layout_above property with the footer id

<LinearLayout
    android:id="@+id/imgFooter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/imgFooter"
    android:layout_below="@id/nav_bar_header"
    android:layout_centerHorizontal="true"
    android:padding="5dp" >
</ScrollView>



回答2:


Use ViewTreeObserver for your layout you used for footer

 ViewTreeObserver vto = layout.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            //Here you can get dimension of view
                        //This method will called after screen created
        }
    });



回答3:


Using View's getLayoutParams() method, and you can get the height from the params :)




回答4:


oldLayout = (android.widget.RelativeLayout.LayoutParams) view.getLayoutParams();
Width = oldLayout.width;


来源:https://stackoverflow.com/questions/13062401/get-the-height-of-layout-which-has-wrap-content-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!