【问题标题】:How to draw a dot circle inside a square drawable in android?如何在android中的正方形drawable内绘制一个圆点?
【发布时间】:2017-04-14 13:37:37
【问题描述】:

我想要一个方形按钮内的小点,这是我到目前为止所尝试过的,由 azizbekian 回答:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle">
            <size android:width="100dp" android:height="100dp"/>
            <solid android:color="#38b0ef"/>
        </shape>
    </item>

    <item
        android:bottom="45dp"
        android:left="45dp"
        android:right="45dp"
        android:top="45dp">
        <shape android:shape="oval">
            <stroke android:width="1dp"  android:color="#0c5069"/>
            <size android:width="20dp" android:height="20dp"/>
            <solid android:color="#0c5069"/>
        </shape>
    </item>


</layer-list>

第一张图片是我尝试过的,第二张图片是我想要的,不包括文字。

这个代码也是我必须应用可绘制对象的按钮 在一个水平线性布局中有七个这样的按钮,weightsum:100 每个按钮的权重为 14

<Button
   android:id="@+id/wed"
   android:layout_width="0dp"
   android:layout_height="35dp"
   android:layout_marginEnd="1dp"
   android:background="@drawable/my_button_dot"
   android:text="WED"
   android:textAllCaps="true"
   android:textStyle="bold"
   android:textColor="#ffffff"
   android:layout_weight="15"/>

当这个drawable应用于按钮时,点不会出现在背景中,可能是因为按钮和drawable的大小我试图调整drawable的大小,但我就是不明白,它没有工作,如何调整它,使点出现在按钮背景中。

【问题讨论】:

  • 为什么需要layer-list?为什么普通的椭圆形不适合你?
  • 我想要蓝色正方形,里面有一个小点,如第二张图片所示。
  • 为什么不将点设置为单独的compoundDrawableBottom 属性?

标签: android android-xml android-drawable xml-drawable android-shapedrawable


【解决方案1】:

首先在drawable中创建color_circle

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="oval">


  <solid android:color="#ffffff">


  </solid>

 <size
    android:width="48dp"
    android:height="48dp" />


</shape>

然后

    <RelativeLayout
    android:layout_width="64dp"
    android:layout_height="148dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:textAllCaps="true"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="WED" />

    </LinearLayout>


    <View
        android:layout_width="16dp"
        android:layout_height="16dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"
        android:background="@drawable/color_circle" />


</RelativeLayout>

我希望这是你想要的

【讨论】:

    【解决方案2】:

    这是

    ImageView 有 src 到 dotted.xml。

    <ImageView
      android:layout_width="178.8dp"
      android:layout_height="178.8dp"
      android:src="@drawable/dotted"
      android:layout_centerInParent="true"
      android:layerType="software" />
    
    add below to drawable/dotted.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
      <stroke
        android:color="@android:color/white"
        android:dashWidth="1px"
        android:dashGap="10px"
        android:width="6.6dp"/>
    </shape>
    

    【讨论】:

    • 缩进变化
    猜你喜欢
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 2015-08-08
    • 2013-09-20
    • 2014-05-04
    • 1970-01-01
    相关资源
    最近更新 更多