Android 如何以编程方式调整自定义视图的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2963152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:04:48  来源:igfitidea点击:

How to resize a custom view programmatically?

androidresize

提问by herbertD

I am coding a custom view, extended from RelativeLayout, and I want to resize it programmatically, How can I do?

我正在编写一个从 RelativeLayout 扩展的自定义视图,我想以编程方式调整它的大小,我该怎么做?

the custom view Class is something like:

自定义视图类类似于:

public ActiveSlideView(Context context, AttributeSet attr){
        super(context, attr);
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(inflater != null){       
            inflater.inflate(R.layout.active_slide, this);
        }

采纳答案by herbertD

this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));

Problem solved!

问题解决了!

NOTE: Be sure to use the parent Layout's LayoutParams. Mine is LinearLayout.LayoutParams!

注意:一定要使用父布局的LayoutParams. 我的是LinearLayout.LayoutParams

回答by smisiewicz

Android throws an exception if you fail to pass the height or width of a view. Instead of creating a new LayoutParams object, use the original one, so that all other set parameters are kept. Note that the type of LayoutParams returned by getLayoutParams is that of the parentlayout, not the view you are resizing.

如果您无法传递视图的高度或宽度,Android 将引发异常。不要创建新的 LayoutParams 对象,而是使用原始对象,以便保留所有其他设置的参数。请注意,getLayoutParams 返回的 LayoutParams 类型是布局的类型,而不是您正在调整大小的视图。

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) someLayout.getLayoutParams();
params.height = 130;
someLayout.setLayoutParams(params);

回答by Sileria

This works for me:

这对我有用:

ViewGroup.LayoutParams params = layout.getLayoutParams();
params.height = customHeight;
layout.requestLayout();

回答by Danield

For what it's worth, let's say you wanted to resize the view in device independent pixels(dp): -

对于它的价值,假设您想以设备独立像素dp)调整视图大小:-

You need to use a method called applyDimension, that's a member of the class TypedValueto convert from pixels to dps. So if I want to set the height to 150dp (say) - then I could do this:

您需要使用一个名为 的方法applyDimension,该方法TypedValue是将像素转换为 dps的类的成员。因此,如果我想将高度设置为 150dp(例如) - 那么我可以这样做:

float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, getResources().getDisplayMetrics());
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) someLayout.getLayoutParams();
params.height = (int) pixels;
someLayout.setLayoutParams(params);

where the expression: getResources().getDisplayMetrics()gets the screen density/resolution

其中表达式:getResources().getDisplayMetrics()获取屏幕密度/分辨率

回答by Phil

In Kotlin, you can use the ktx extensions:

在 Kotlin 中,您可以使用ktx 扩展

yourView.updateLayoutParams {
   height = <YOUR_HEIGHT>
}

回答by atroutt

Here's a more generic version of the solution above from @herbertD :

这是@herbertD 上面解决方案的更通用版本:

private void resizeView(View view, int newWidth, int newHeight) { 
    try { 
        Constructor<? extends LayoutParams> ctor = view.getLayoutParams().getClass().getDeclaredConstructor(int.class, int.class); 
        view.setLayoutParams(ctor.newInstance(newWidth, newHeight));   
    } catch (Exception e) { 
        e.printStackTrace(); 
    }
}

回答by zed_0xff

try a this one:

试试这个:

...
View view = inflater.inflate(R.layout.active_slide, this);
view.setMinimumWidth(200);

回答by AkhilGite

I used this way to increase width of custom view

我用这种方式来增加自定义视图的宽度

customDrawingView.post(new Runnable() {
                            @Override
                            public void run() {
                                View view_instance = customDrawingView;
                                android.view.ViewGroup.LayoutParams params = view_instance
                                        .getLayoutParams();
                                int newLayoutWidth = customDrawingView
                                        .getWidth()
                                        + customDrawingView.getWidth();
                                params.width = newLayoutWidth;
                                view_instance.setLayoutParams(params);
                                screenWidthBackup = params.width;
                            }
                        });

回答by DazzleGifts Apps

I solved it this way.. I have basically a simple view inside xml file.

我是这样解决的。我在 xml 文件中基本上有一个简单的视图。

 View viewname = findViewById(R.id.prod_extra);
             prodExtra.getLayoutParams().height=64;

回答by Xar-e-ahmer Khan

If you have only two or three condition(sizes)then you can use @OverideonMeasurelike

如果你只有two or three condition(sizes)那么你可以使用@OverideonMeasure

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
{
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

And change your size for these conditions in CustomView class easily.

并在 CustomView 类中轻松更改这些条件的大小。