【问题标题】:How to add another ImageView over an existing ImageView dynamically?如何在现有 ImageView 上动态添加另一个 ImageView?
【发布时间】:2017-02-25 17:50:59
【问题描述】:

我知道以前有人问过类似的问题,但我认为我的情况有所不同。 So I'm trying to make a playlist using a RecyclerView and when a song is selected I want the circular image which holds the Image for the song to change to a tick sign while the original image can be seen faded in the background.我已经准备好了刻度图。

如何在现有的 ImageView 上动态添加此图像?将一张图片放在后台的方法对我来说不是一个解决方案,因为音乐图片文件是从 Realm 动态加载的,因此不能作为背景。

我的 RecyclerView 项目的 XML 代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingRight="10dp"
android:paddingLeft="30dp"
>

<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="start"
android:transitionName="albumArt"
android:id="@+id/album_art"
android:scaleType="centerCrop"
android:src="@color/colorPrimary"/>


<LinearLayout
    android:layout_width="fill_parent"
    android:gravity="right"
    android:paddingLeft="20dp"
    android:orientation="vertical"
    android:layout_marginTop="20dp"
    android:layout_height="wrap_content">
    <TextView
     android:id="@+id/data"
      android:layout_width="match_parent"
      android:fontFamily="sans-serif"
      android:textColor="#ffffff"
      android:text="Music Name"
      android:textSize="17sp"
      android:layout_height="wrap_content"
     />
    <TextView
     android:id="@+id/data_album"
     android:layout_width="match_parent"
     android:fontFamily="sans-serif"
     android:textColor="#bfe0e0e0"
     android:textSize="14sp"
     android:text="Music Artist"
     android:layout_height="wrap_content"
    />
</LinearLayout>

如何从 java 代码中动态执行此操作?这可以以某种方式使用set visibility 来完成吗?如果是,请解释执行此操作的新方法。

【问题讨论】:

  • use LayerDrawable class: "管理其他 Drawable 数组的 Drawable。这些是按数组顺序绘制的,因此索引最大的元素将绘制在顶部。"

标签: android xml imageview


【解决方案1】:

您可以在此处使用FrameLayout 并将ImageViews 放在FrameLayout 中,根据条件您可以将其中一个设为可见,而将另一个设为不可见

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingRight="10dp"
android:paddingLeft="30dp"
>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="start"
android:transitionName="albumArt"
android:id="@+id/album_art"
android:scaleType="centerCrop"
android:src="@color/colorPrimary"/>

<ImageView
android:id="@+id/imageView"
android:layout_width="120dp"
android:layout_height="120dp">
</ImageView>
</FrameLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:gravity="right"
    android:paddingLeft="20dp"
    android:orientation="vertical"
    android:layout_marginTop="20dp"
    android:layout_height="wrap_content">
    <TextView
     android:id="@+id/data"
      android:layout_width="match_parent"
      android:fontFamily="sans-serif"
      android:textColor="#ffffff"
      android:text="Music Name"
      android:textSize="17sp"
      android:layout_height="wrap_content"
     />
    <TextView
     android:id="@+id/data_album"
     android:layout_width="match_parent"
     android:fontFamily="sans-serif"
     android:textColor="#bfe0e0e0"
     android:textSize="14sp"
     android:text="Music Artist"
     android:layout_height="wrap_content"
    />
</LinearLayout>

【讨论】:

    【解决方案2】:

    是的,可以通过使用设置可见性来完成。在您的布局文件中,您很可能需要使用相对布局,这样您就可以将两个图像视图锚定完全相同。然后在您最初不想显示的视图上,通过添加以下内容使其不可见:

    android:visibility="invisible"
    

    然后当您想要打开/关闭可见性时,您可以使用:

    myImageView.setVisibility(View.VISIBLE);
    myImageView.setVisibility(View.INVISIBLE);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 2014-12-31
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多