android开发分享如何以编程方式调整自定义视图的大小?

我正在编写自定义视图,从RelativeLayout扩展,我想要以编程方式调整它,我该怎么办?

自定义视图类是这样的:

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

    如果您错过传递布局的高度或宽度,Android会引发exception。 而不是创build一个新的LayoutParams对象使用视图的原始的,这样的所有其他设置参数保留:

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

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

    问题解决了!

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

    这适用于我:

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

    对于什么是值得的,比方说,你想调整设备无关像素dp )的视图:

    您需要使用一个名为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); 

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

    试试这个:

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

    以下是来自@herbertD的更通用的解决scheme:

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

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

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

    我解决了这个问题..我基本上有一个简单的视图内的XML文件。

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

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

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

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

    如果你重写onMeasure ,不要忘记更新新的尺寸

     @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(newWidth, newHeight); } 

      以上就是android开发分享如何以编程方式调整自定义视图的大小?相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/516825.html

      (0)
      上一篇 2020年12月3日
      下一篇 2020年12月3日

      精彩推荐