【问题标题】:How to draw a perfect circular button?如何画一个完美的圆形按钮?
【发布时间】:2013-12-11 04:17:30
【问题描述】:

我想画一个完美的圆形按钮。 我尝试使用以下代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <stroke 
        android:width="3dip"
        android:color="#065a32" />
    <corners android:radius="10dip"/>
      <solid android:color="#eaebec" />
</shape>

但我只能画一个椭圆形,即使我改变了参数:

<stroke 
android:width="3dip"/>
<corners android:radius="10dip"/>

我的截图:

我已经尝试过这个网站的不同链接,但没有一个能满足我的需要。

编辑:

我的 getView() 代码是:

public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (convertView == null) { 
            LayoutInflater vi = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.calendar_item, null);

        }
view.setBackgroundResource(R.drawable.calendar_cell_clicked);
}

【问题讨论】:

  • 您可以在photoshop中绘制一个圆形图像并将其设置为按钮的背景。可以根据屏幕大小改变图像的分辨率。

标签: android android-xml android-button


【解决方案1】:

使用这个xml

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

  <solid
      android:color="#eaebec" />
</shape>

在你设置背景的布局中使用

        android:layout_width="50dp"
        android:layout_height="50dp"

或您需要的任何大小的圆圈。

【讨论】:

  • 感谢您的回答我尝试了您的代码,但仍然没有得到完美的循环。我也不能给我的视图提供常量参数,即它会根据屏幕大小而变化。
  • 如果它根据屏幕尺寸变化,则根据屏幕尺寸在dimens.xml中定义尺寸。
  • 解决方案太麻烦了,即用一些尺寸值替换 android:layout_width="wrap_content" android:layout_height="wrap_content"。我认为这不是一个完美的解决方案。你正在制作它太复杂了。考虑我是否需要用不同的按钮大小来做。
  • maon 显然,如果您希望它是圆形的,则必须提供相等的高度和宽度。如果要在椭圆形 xml 中定义高度宽度,可以在 shape 中编写
  • 我的高度和宽度总是等于我将按钮放在gridview中。
【解决方案2】:

使用这个xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#9F2200"/>
    <stroke android:width="2dp" android:color="#fff" />
</shape>

在你的布局中

android:background="@drawable/button"

【讨论】:

    【解决方案3】:

    在您的可绘制文件夹中创建以下内容

    circular_button.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval"
        android:innerRadius="15dp"
        android:thickness="10dp"
        android:useLevel="false">
    
        <solid android:color="#ce2c2c" />
    
    </shape>
    

    将以下属性添加到任何按钮以使其成为圆形

    android:background="@drawable/circular_button"
    

    输出

    【讨论】:

      猜你喜欢
      • 2016-09-25
      • 2017-03-04
      • 2017-09-13
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      • 1970-01-01
      相关资源
      最近更新 更多