Android ImageView可以';不要在布局中的正确位置放置

Android ImageView可以';不要在布局中的正确位置放置,android,android-layout,android-imageview,android-relativelayout,Android,Android Layout,Android Imageview,Android Relativelayout,我想制作一个购物车图像,右上角有一个小指示器,显示选择/订购了多少产品。 问题是,我不能把它放在右上角,只能放在左上角(默认值)或中间 <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/shoppingCartIcon"

我想制作一个购物车图像,右上角有一个小指示器,显示选择/订购了多少产品。 问题是,我不能把它放在右上角,只能放在左上角(默认值)或中间

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

         <ImageView
              android:id="@+id/shoppingCartIcon"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/icon_cart" />

         <ImageView
              android:id="@+id/indicatorIcon"
              android:layout_width="10dp"
              android:layout_height="10dp"
              android:layout_gravity="top|right"
              android:src="#0066ff" />

</RelativeLayout>

您可以将此代码添加到ImageView标记中

android:layout_marginLeft="90dp"

它将使ImageView向右边缘左侧显示空白,具体取决于所需的大小。您可以根据需要更改大小

通过使用FrameLayout,您可以为它添加一些填充和边距

<FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/shoppingCartIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_cart" />

        <ImageView
            android:id="@+id/indicatorIcon"
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:layout_gravity="top|right"
            android:src="#0066ff" />

    </FrameLayout>

更新


您可以根据自己的愿望创建形状

res/drawable/circle\u shape.xml


除此之外,我建议您使用其他第三个库,它非常适合显示这种类型的徽章或突出显示的视图


父对象是一个
相对对象,因此请尝试使用以下属性:

android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
在您的
“@+id/indicationCon”

希望能有所帮助。


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

<ImageView
    android:id="@+id/shoppingCartIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon_cart" />

<ImageView
    android:id="@+id/indicatorIcon"
    android:layout_width="10dp"
    android:layout_height="10dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:src="#0066ff" />

</RelativeLayout>

您知道如何更改指示灯上的数字文本吗?(根据订单)?我必须在顶部使用TextView吗?或者只是向ImageView本身添加文本就可以了?
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/shoppingCartIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon_cart" />

<ImageView
    android:id="@+id/indicatorIcon"
    android:layout_width="10dp"
    android:layout_height="10dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:src="#0066ff" />

</RelativeLayout>