上的一个按钮的左上侧定位徽章气泡气泡、徽章、按钮

2023-09-06 04:15:49 作者:这条咸鱼我来保护

我想徽章添加到我的活动的按钮之一。现在我要做的XML。 用徽章的按钮,应该是这样的:

我已经做了泡,用里面的文字 RelativeLayout的

 < RelativeLayout的机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT>

            < ImageView的机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:adjustViewBounds =真
                机器人:SRC =@可绘制/徽章
                机器人:layout_centerInParent =真/>

             < TextView的Andr​​oid的:layout_width =WRAP_CONTENT
                       机器人:layout_height =WRAP_CONTENT
                       机器人:TEXTSTYLE =黑体
                       机器人:文本=2
                       机器人:layout_centerInParent =真/>
         < / RelativeLayout的>
 

但我不能找到一种方法,将它放在这里,使它在纵向和横向与同一个XML工作。

在Buttoms在活动是这样的:

 <按钮机器人:ID =@ + ID / new_releases_button
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=@可绘制/ ic_button_selector
            机器人:文本=@字符串/ new_releases_title
            机器人:文字颜色=#FFFFFF
            机器人:TEXTSIZE =20SP
            机器人:重力=center_vertical
            机器人:以下属性来=12dp
            机器人:layout_marginTop =15dp
            机器人:layout_below =@ ID / coming_soon_button
            机器人:的onClick =newReleaseClick
            机器人:layout_centerHorizo​​ntal =真/>

        <按钮机器人:ID =@ + ID / top_sellers_button
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=@可绘制/ ic_button_selector
            机器人:文本=@字符串/ top_sellers_title
            机器人:文字颜色=#FFFFFF
            机器人:TEXTSIZE =20SP
            机器人:重力=center_vertical
            机器人:以下属性来=12dp
            机器人:layout_marginTop =15dp
            机器人:layout_below =@ ID / new_releases_button
            机器人:的onClick =topSellersClick
            机器人:layout_centerHorizo​​ntal =真/>
 

和这里有两个资源:

我应该怎么做的XML?

编辑: 最好的方法,到目前为止,但它仍然无法正常工作:

 < XML版本=1.0编码=UTF-8&GT?;

<的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>

        <按钮机器人:ID =@ + ID / discounts_button
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:背景=@可绘制/ ic_button_selector
                机器人:文本=@字符串/ discounts_title
                机器人:文字颜色=#FFFFFF
                机器人:TEXTSIZE =20SP
                机器人:的onClick =discountsClick
                机器人:layout_marginTop =40dp
                机器人:layout_marginLeft =20dp/>

        < RelativeLayout的机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_gravity =顶|留下>

            < ImageView的机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:adjustViewBounds =真
                    机器人:SRC =@可绘制/徽章
                    机器人:layout_centerInParent =真/>

             < TextView的Andr​​oid的:layout_width =WRAP_CONTENT
                           机器人:layout_height =WRAP_CONTENT
                           机器人:TEXTSTYLE =黑体
                           机器人:文本=20
                           机器人:layout_centerInParent =真/>
         < / RelativeLayout的>
< /的FrameLayout>
 
如图是一把游戏锁,上面有16个按钮.游戏规则如下 按照按钮上的提示,按遍全部按钮,才能把锁打开.比如

解决方案

使用的FrameLayout (而不是RelativeLayout的)一个,并把按钮和图像进去。

通过

位置的图像(cirle带号)和按钮

 安卓layout_gravity =顶|左
机器人:layout_marginTop =XDP
机器人:layout_marginLeft =XDP
 

要你的喜好。

I am trying to add a badge to one of my Activity's button. Right now I am trying to do the xml. The Button with the badge, should look like this:

I have already done the bubble and the text inside using a RelativeLayout:

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

            <ImageView android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/badge"
                android:layout_centerInParent="true"/>

             <TextView android:layout_width="wrap_content" 
                       android:layout_height="wrap_content"
                       android:textStyle="bold"
                       android:text="2"
                       android:layout_centerInParent="true" />
         </RelativeLayout>

But I can't find a way to place it there and making it work on portrait and landscape with the same xml.

The Buttoms in the Activity are like this:

<Button android:id="@+id/new_releases_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_button_selector"
            android:text="@string/new_releases_title"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            android:gravity="center_vertical"
            android:paddingLeft="12dp"
            android:layout_marginTop="15dp"
            android:layout_below="@id/coming_soon_button"
            android:onClick="newReleaseClick"
            android:layout_centerHorizontal="true" />

        <Button android:id="@+id/top_sellers_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_button_selector"
            android:text="@string/top_sellers_title"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            android:gravity="center_vertical"
            android:paddingLeft="12dp"
            android:layout_marginTop="15dp"
            android:layout_below="@id/new_releases_button"
            android:onClick="topSellersClick"
            android:layout_centerHorizontal="true" />

and here are the two resources:

How should I do the xml?

EDIT: Best approach so far, but it still doesn't work:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button android:id="@+id/discounts_button"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:background="@drawable/ic_button_selector"
                android:text="@string/discounts_title"
                android:textColor="#FFFFFF"
                android:textSize="20sp"
                android:onClick="discountsClick"
                android:layout_marginTop="40dp"
                android:layout_marginLeft="20dp"/>

        <RelativeLayout android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_gravity="top|left">

            <ImageView android:layout_width="wrap_content" 
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:src="@drawable/badge"
                    android:layout_centerInParent="true"/>

             <TextView android:layout_width="wrap_content" 
                           android:layout_height="wrap_content"
                           android:textStyle="bold"
                           android:text="20"
                           android:layout_centerInParent="true" />
         </RelativeLayout>
</FrameLayout>

解决方案

Use a FrameLayout (instead of RelativeLayout) and put button and image into it.

Position the image (cirle with number) and button via

android:layout_gravity="top|left"
android:layout_marginTop="Xdp"
android:layout_marginLeft="Xdp"

to your likes.