【问题标题】:ImageView doesn't wrap content in RelativeLayoutImageView 不在 RelativeLayout 中包装内容
【发布时间】:2013-08-07 18:03:11
【问题描述】:

我有一个包含 6 个子项/条目的 TableLayout。这些孩子是一个自定义的RelativeLayout。在每个RelativeLayout中,中间是一个大TextView,底部是一个ImageView和一个小TextView。

ImageView 应该和它旁边的 TextView 一样高。这就是我将属性 ALIGN_TOP 和 ALIGN_BOTTOM 设置为 TextView 的原因(您可以在下面的代码中看到它)。这很好用,而且 ImageView 和 TextView 现在都具有相同的高度。但问题是,ImageView 的左侧和右侧不再“包装内容”(如您在屏幕截图中所见)。

有没有办法让图像的左侧和右侧适合并删除“填充”?

这是我的代码:

view_display_component.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >   
<TextView
    android:id="@+id/tvDisplayBig"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_weight="1"
    android:gravity="center"
    android:textColor="@color/white"
    android:textSize="@dimen/font_size_extra_large" />

<ImageView
    android:id="@+id/imageViewDisplayIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/tvDisplayBig"
    android:layout_gravity="bottom"
    android:adjustViewBounds="true"
    android:baselineAlignBottom="true"
    android:scaleType="fitCenter"
    android:src="@drawable/stopwatch_64"
    android:visibility="visible" />

<TextView
    android:id="@+id/tvDisplaySmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"  
    android:layout_alignParentBottom="true"
    android:gravity="bottom"
    android:includeFontPadding="false"
    android:textColor="@color/white"
    android:textSize="@dimen/font_size_small" />                
</merge>

扩展 RelativLayout 的类 DisplayComponent

public DisplayComponent(Context context) {
    super(context);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.view_display_component, this, true);
    tvDisplay = (TextView) getChildAt(0);
    icon = (ImageView) getChildAt(1);
    tvName = (TextView) getChildAt(2);

    setupAlign();
}

private void setupAlign() {
    if(index % 2 == 0) {    // LEFT SIDE
       // same as "RIGHT SIDE"
    } else {        // RIGHT SIDE
        RelativeLayout.LayoutParams paramsIcon = (RelativeLayout.LayoutParams) icon.getLayoutParams();
        paramsIcon.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        paramsIcon.addRule(RelativeLayout.ALIGN_TOP, tvName.getId());
        paramsIcon.addRule(RelativeLayout.ALIGN_BOTTOM, tvName.getId());
        icon.setLayoutParams(paramsIcon);
        RelativeLayout.LayoutParams paramsTvName = (RelativeLayout.LayoutParams) tvName.getLayoutParams();
        paramsTvName.addRule(RelativeLayout.RIGHT_OF, icon.getId()); 
        tvName.setLayoutParams(paramsTvName);

        tvName.setBackgroundColor(Color.BLUE); // only for testing
        icon.setBackgroundColor(Color.YELLOW);
    }

【问题讨论】:

  • 你的那个闹钟的原始图像有那个填充吗?
  • 一种解决方案是尝试为您的图像视图提供宽度和高度,并将您的 scaleType 更改为 fitXY。
  • 不,ImageView 没有填充,我使用设置“icon.setPadding(0,0,0,0)”对其进行了测试。您的解决方案的问题是,我无法获得 ImageView 或 TextView 的宽度/高度。调用“getHeight”或“getMeasuredHeight”总是返回值 0。我认为在构造函数中获取视图的大小还为时过早。 (顺便说一句,图标的原始大小是 64x64 像素。)
  • 试试这个,而不是 layout_width 和 height 设置为 wrap_content 的 imageview,设置为 64 dip。看看会发生什么。
  • 不,没有任何改变。结果和以前一样。必须有一种方法来设置 aign_top/align_bottom 并且 ImageView 会自行调整 align_left/align_right 的大小...

标签: java android alignment android-imageview android-relativelayout


【解决方案1】:

我找到了一个(丑陋的)解决方案。因为我的图标是方形的,所以我创建了一个自定义 ImageView 并像这样覆盖 onSizeChanged() 方法:

public class IconImageView extends ImageView {

    public IconImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if(h != oldh && h > 0)
            getLayoutParams().width = h;  // same width as height
    }
}

但这只有在图像是正方形时才有效。这就是为什么我仍在寻找更好的解决方案。也许一些布局解决方案具有更好的对齐设置。

最好的问候!

【讨论】:

    猜你喜欢
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多