简体   繁体   English

如何使用layout_weight获取LinearLayout的高度(以像素为单位)

[英]How to get height in pixels of LinearLayout with layout_weight

this is my first post. 这是我的第一篇文章。 I am learning to code in java for android apps. 我正在学习java for Android应用程序的代码。 I am using a LinearLayout "variable named: ll" with a predefined layout_weight (which I want to change whenever I want in the future) and height and width set to MATCH_PARENT. 我正在使用一个LinearLayout“变量名为:ll”,带有预定义的layout_weight(我想在将来随时更改)并且高度和宽度设置为MATCH_PARENT。 I want to get the height of this LinearLayout in pixels. 我想以像素为单位获得此LinearLayout的高度。 I fill this using java (ll.addView(variableForAnObject);). 我使用java(ll.addView(variableForAnObject);)填充它。 I am trying to get its height using ll.getLayoutParams().height; 我试图使用ll.getLayoutParams()。height获得它的高度; but I am getting -1. 但我得到了-1。 Because I believe this is the constant for MATCH_PARENT. 因为我相信这是MATCH_PARENT的常量。 Setting the height to 0dp makes the ll invisible and gives height as 0. 将高度设置为0dp会使ll不可见,并将高度设置为0。

Please tell me if there's a way to get its height. 请告诉我是否有办法达到它的高度。 I think I can do this by getting screen height using DisplayMetrics and making nearly impossible calculations (for me, at least) but this will be too complex. 我想我可以通过使用DisplayMetrics获取屏幕高度并进行几乎不可能的计算(至少对我而言)来做到这一点,但这太复杂了。

try with ll.getHeight() should work 尝试用ll.getHeight()应该工作

if not, it's because android has not yet calculed its bounds, then: 如果没有,那是因为android还没有计算出它的界限,那么:

//inside of onCreate method should work
ll.post(new Runnable() {
    @Override
    public void run() {
        //maybe also works height = ll.getLayoutParams().height;

        height  = ll.getHeight();

    }
});

It is -1 because its dimensions have not yet been set, whenever you set layout it takes some time to compute the actual values including height, width etc. 它是-1,因为它的尺寸尚未设置,每当你设置布局时,它需要一些时间来计算实际值,包括高度,宽度等。

Use OnLayoutListener to listen when it has finished with the layout. 完成布局后,使用OnLayoutListener进行侦听。

layout.onLayoutChange (View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
    //Extract values here
    height = view.getHeight();
    width = view.getWidth()
}

See this for more about the OnLayoutListener 为更多关于OnLayoutListener

Define your LinearLayout in your class and use getHeight() and getWidth(). 在类中定义LinearLayout并使用getHeight()和getWidth()。 Example: 例:

LinearLayout yourLayout = (LinearLayout) findViewById (R.id.yourLL);
double height = yourLayout.getHeight();
double width = yourLayout.getWidth();

Hope it helps! 希望能帮助到你! :) :)

Give the id field in your layout xml file: 在布局xml文件中给出id字段:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:id="@+id/<ID>>

Use this id to get reference in java class and find height and width: 使用此id在java类中获取引用并查找高度和宽度:

LinearLayout linearLayout= (LinearLayout) findViewById (R.id.<ID>);
double height = linearLayout.getHeight();
double width = linearLayout.getWidth();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM