GridView单元格的高度以填充小部件的大小

时间:2018-07-07 17:00:00

标签: android gridview android-appwidget

我有 4行,我想与小部件高度动态匹配。 每行将获得小部件总高度的25%

我没有运气尝试过的事情:

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.gridholder);
Intent intent = new Intent(context.getApplicationContext(), Gridfactory.class);
resizedGrid item = (resizedGrid) ((Activity) context).findViewById(R.layout.grid_view);
views.setRemoteAdapter(item.getId(), intent);


    private class resizedGrid extends GridView {

        public resizedGrid(Context context) {
            super(context);
        }

        public resizedGrid(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public resizedGrid(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int heightSpec;

            if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
                heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            } else {
                heightSpec = heightMeasureSpec;
            }

            super.onMeasure(widthMeasureSpec, heightSpec);
        }
    }

0 个答案:

没有答案