简体   繁体   English

Android:可以在绘制视图之前更改视图的高度吗?

[英]Android: Can I change the height of a view before it's drawn?

I'd like to do something like the following: 我想做以下事情:

rootLayout.getLayoutParams().height = 100;

Currently I have this line in my 'loadData' method. 目前,我的“ loadData”方法中包含此行。 The problem is - 'layoutParams' seems to be null until sometime after 'loadData' has been called (but before it is displayed obviously). 问题是-“ layoutParams”似乎为空,直到调用了“ loadData”之后的某个时间(但是在明显显示之前)。

Is there somewhere I can place this line where the layoutParams will have been instantiated, but still be before the view is shown for the first time? 有什么地方可以放置此行的位置,该行将实例化layoutParams,但仍在第一次显示视图之前?

    rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    rootLayout.getViewTreeObserver()
                            .removeOnGlobalLayoutListener(this);
                } else {
                    rootLayout.getViewTreeObserver()
                            .removeGlobalOnLayoutListener(this);
                }
                rootLayout.getLayoutParams().height = 100;
                rootLayout.requestLayout();
            }
        });

You should consider to set layoutParams of this view as 您应该考虑将此视图的layoutParams设置为

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 100));
 rootLayout.setLayoutParams(lp);

So You can set layoutParams height as 100 and assuming that your parent view as linear layout ,if it's not, then you should use its layoutparams 因此,您可以将layoutParams的高度设置为100,并假设您的父视图为linear layout ,否则请使用其layoutparams

on the docs you can check all the callbacks the view have that you can override https://developer.android.com/reference/android/view/View.html 在文档上,您可以检查视图中所有的回调,您可以覆盖https://developer.android.com/reference/android/view/View.html

if your view is inside an XML layout with the LayoutParams specified in there, you can/should put your code inside onFinishInflate 如果您的视图位于其中指定了LayoutParams的XML布局内,则可以/应该将代码放在onFinishInflate中

   @Override
   public void onFinishInflate() {
   }

if you're doing all this programmatically, you can override the layout pass 如果您以编程方式执行所有操作,则可以覆盖版式传递

@Override
public void onLayout (boolean changed, int left, int top, int right, int bottom){
    // be carefull that this get's called several times
}

alternatively (a nicer approach in my opinion), you can trick the onMeasure of your view to have the size you want. 或者(我认为这是一种更好的方法),您可以欺骗视图的onMeasure使其具有所需的大小。 For example, to have a view with a maxWidth 例如,要使视图具有maxWidth

   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      // apply max width
      int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
      if (maxWidth > 0 && maxWidth < measuredWidth) {
         int measureMode = MeasureSpec.getMode(widthMeasureSpec);
         widthMeasureSpec = MeasureSpec.makeMeasureSpec(maxWidth, measureMode);
      }
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
   }

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

相关问题 如何在绘制之前获取调整大小的自定义视图的宽度和高度 - How to get width and height of resized custom view , before it is drawn Android:我绘制了许多形状。 每当我单击形状时,都需要更改其颜色 - Android: I have many shapes drawn. I need to change a shape's color whenever I click on it 在Libgdx(Android)的Camera视图之外,是否会自动不绘制精灵 - Will the sprite be automatically not drawn when outside the Camera's view in Libgdx (Android) 在Android中,如何设置ListView项的高度和宽度? - In Android, how can I set a ListView item's height and width? 在Android中更改自定义视图宽度和高度 - Change Custom View Width And Height In Android 根据ConstraintLayout中另一个视图的高度更改高度百分比 - Change height percentage based on another view's height in ConstraintLayout Android:setBackground 方法更改视图的高度 - Android : setBackground method changes View's height 如何测量宽度和高度,然后在添加视图之前更改参数? - How to measure width and height, then change parameters before adding view? 我是否必须使用动画来更改Android活动的视图? - Do I have to use an animation to change an Android activity's view? 我正在尝试用Java做一个沙漏,因为ASCII艺术的高度由我可以更改的变量控制 - I am trying to make an hourglass in java as ASCII art who's height is controlled by a variable that I can change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM