繁体   English   中英

如何以编程方式在ImageView中显示多个可绘制对象(带有边距)

[英]How to display multiple drawables (with margins) inside ImageView programatically

是否可以在ImageView中显示多个可绘制对象(它们之间有边距),而无需使用框架布局和任何不必要的嵌套? 如何实现跟进?

  1. 每个可绘制对象都将正常显示而无需调整大小
  2. drawableAdrawableB的末端/右边有10dp的边距

XML

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/myImgView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/myTxtView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Java的

        Resources r = getContext().getResources();
        int tenDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, r.getDisplayMetrics());

        ImageView customIV = content.findViewById(R.id.myImgView);
        Drawable drawableA = getResources().getDrawable(R.drawable.ic_happyface);
        Drawable drawableB = getResources().getDrawable(R.drawable.ic_neutralface);
        Drawable drawableC = getResources().getDrawable(R.drawable.ic_sadface);
        customIV.addView(?);

ImageView仅包含一个Drawable 需要多个ImageViews才能显示多个Drawable

您可以用RelativeLayout替换根目录LinearLayout以避免嵌套。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM