【问题标题】:Resize child view based on parent width and height根据父宽度和高度调整子视图的大小
【发布时间】:2016-06-18 11:08:03
【问题描述】:

我已经创建了自定义视图组。

<com...Template
            android:id="@+id/template"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_weight=".5" />

我在运行时动态添加到上面的布局。

Template.java

 currentTemplateView = LayoutInflater.from(context).inflate(R.layout.template_one, this, false);

addView(currentTemplateView, 0);
@Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        Log.e("Onla", "" + l + t);
        int row, col, left, top;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
        }

        // addView(currentTemplateView, 0);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int mode = MeasureSpec.getMode(widthMeasureSpec); // mode == View.MesaureSpec.EXACTLY
        int size = MeasureSpec.getSize(widthMeasureSpec); // size == 400
        Log.e("onmeasure", "Width: " + mode + "," + size);
        for (int i = 0; i < getChildCount(); i++) {
            measureChildWithMargins(getChildAt(i), widthMeasureSpec, 0, heightMeasureSpec, 0);

            // getChildAt(i).measure(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

高度为 289 dp(模板)的普通布局。显示不错。。

当我在列表或网格视图中使用时。视图组中的子项已折叠。它应该适合父视图组(template.java)。

我希望视图组看起来像第一个图像,无论我做什么调整大小..让我在我的代码中需要更改什么或对此有任何想法..

提前致谢。

【问题讨论】:

    标签: android listview gridview viewgroup


    【解决方案1】:

    最后我做了我需要的......所以我只是缩放模板视图

         if (isScaleLayout) {
    
                   view.setScaleX(scaleX);/0-1
                    view.setScaleY(scaleY);/0-1
                    view.setPivotX(0f);
                    view.setPivotY(getWidth());
                }
    **XML** 
    
     <FrameLayout
            android:layout_width="150dp" // Actually size for layout/ adapter  view
            android:layout_height="80dp"
            android:padding="3dp">
    
            <com..Template
                android:id="@+id/template"
                android:layout_width="@dimen/template_height"//205
                android:layout_height="@dimen/template_height"//205 />
    
    
        </FrameLayout>
    

    【讨论】:

      猜你喜欢
      • 2017-12-08
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多