Superpose many image View on an ImageView in a cus

2019-09-01 04:49发布

问题:

I would like to create a view in android that componsed of an ImageView and a TextView upon this ImageView. But the Custom views that we can create in Android contain attributes that can only be of certain types, as specified in this post : Defining custom attrs So I can't define a custom view with an ImageView attribute and a TextView attribute. I don't want to define it this way neither :

<FrameLayout android:id="@+id/frame_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

      <ImageView
         android:id="@+id/card_11"
         android:layout_width="@dimen/card_width"
         android:layout_height="@dimen/card_height"
         android:src="@drawable/carte_vierge_bleue"
         android:scaleType="fitXY"
         android:clickable="true" 
         android:visibility="visible"
         android:onClick="onCardClicked"/>

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1234 5678 1234 5678"
        android:layout_gravity="center"/>

</FrameLayout>

Because I would like to add other images on the first ImageView after that so I would like to have a custom view that I can modify however I want, and add on it as many ImageViews as I want, and this way of doing would make the layout file very long... Any idea ?

回答1:

If i understood correctly, you can use a RelativeLayout with as many ImageViews as you want. For example:

<RelativeLayout android:id="@+id/frame_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

      <ImageView
         android:id="@+id/card_11"
         android:layout_width="@dimen/card_width"
         android:layout_height="@dimen/card_height"
         android:src="@drawable/carte_vierge_bleue"
         android:scaleType="fitXY"
         android:clickable="true" 
         android:visibility="visible"
         android:onClick="onCardClicked"/>

     <ImageView
         android:id="@+id/card_12"
         android:layout_width="@dimen/card_width"
         android:layout_height="@dimen/card_height"
         android:src="@drawable/carte_vierge_bleue"
         android:scaleType="fitXY"
         android:clickable="true" 
         android:visibility="visible"
         android:onClick="onCardClicked"/>

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1234 5678 1234 5678"
        android:layout_gravity="center"/>

</RelativeLayout>

The TextView will be displayed on top of card12 ImageView, which will be displayed on top of card11 ImageView and so on.

Hope it helped, Mattia