【问题标题】:Android - ImageView margins not appearing in LinearLayoutAndroid - ImageView 边距未出现在 LinearLayout 中
【发布时间】:2011-09-28 22:28:05
【问题描述】:

现在,我正在努力完成一些简单的事情,例如在自定义 LinearLayout 中添加我的子 ImageView 之间的边距空间(修改后的 RadioGroup 旨在接收实现 Checkable 的自定义 ImageView,没有覆盖 onMesarue)。长话短说,这些图片的尺寸是固定的(60x60dip),而且由于它们是动态的(来自网络),我不得不像这样动态地添加它们:

        for(int i = 0; i < num; i++){
            ImageViewRadioButton childImage = new ImageViewRadioButton(mContext);
            float imagehWidthHeight = getResources().getDimension(R.dimen.image_width_and_height);

            LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams((int) imageWidthHeight, (int) imageWidthHeight);
            int imageSpacing = Utils.dipsToPixels(10, mContext);
            int innerPadding = Utils.dipsToPixels(5, mContext);

            imageParams.leftMargin = imageSpacing;
            imageParams.rightMargin = imageSpacing;
            childImage.setAdjustViewBounds(true);
            childImage.setLayoutParams(imageParams);
            childImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.blue_pressed));
            childImage.setPadding(innerPadding, innerPadding, innerPadding, innerPadding);
            childImage.setClickable(true);
            //other non-image properties...
            imageContainer.addView(childImage);
        }

唯一起作用的是填充,它可以将其适当地隔开。但是,我没有看到每个孩子的填充(边距)之间有任何空间。我这样做是否正确,或者有没有更好的方法来代替覆盖 onMeasure 以考虑每个孩子的边距?

【问题讨论】:

    标签: android imageview android-linearlayout margins layoutparams


    【解决方案1】:

    您已经创建了imageParams,但您没有在代码中使用该参数而不是imageParams,您正在使用swatchParams 参数。而且您还没有输入swatchParams 参数的代码。

    【讨论】:

    • 显然,这是一个例子,所以应该是 imageParams。即使这样,当我创建 LayoutParams 对象并将其传递给视图时,它仍然无法正常工作。
    猜你喜欢
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2019-07-05
    • 2015-01-30
    • 2013-11-01
    • 2018-08-10
    相关资源
    最近更新 更多