Superpose many image View on an ImageView in a customView

佐手、 提交于 2019-12-13 05:17:46

问题


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



来源:https://stackoverflow.com/questions/23564824/superpose-many-image-view-on-an-imageview-in-a-customview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!