【问题标题】:Can`t round the corners of the button无法圆角按钮
【发布时间】:2017-12-13 14:44:47
【问题描述】:

我写了下面的代码,但我的 ImageButton 仍然有尖角。也许我不知道什么,如果你知道,请告诉我。

我的观点

<ImageButton
    android:id="@+id/bottle"
    android:src="@drawable/bottle"
    android:background="@drawable/rouncorners"
    android:layout_width="100dp"
    android:layout_height="100dp"/>

可绘制的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"/>
</shape>

【问题讨论】:

    标签: android imagebutton


    【解决方案1】:

    好的,我在我的 Studio 上测试了我的启动器图标作为里面的图像,我得到了所需的结果。这是整个 xml:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
    
    <ImageButton
        android:id="@+id/bottle"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:contentDescription="@null"
        app:srcCompat="@mipmap/ic_launcher"
        android:background="@drawable/rouncorners"/>
    
    </android.support.constraint.ConstraintLayout>
    

    而要放入drawable文件的xml是:

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <stroke android:width="3dip" android:color="@color/profile_cover" />
        <corners android:radius="10dp" />
     </shape>
    

    结果如下:

    这就是您要找的东西,对吗?它有圆角和内部图像。

    【讨论】:

    • 不行,ImageView、TextView都一样的结果。
    • 在第一种情况下是尖角,在第二种情况下按钮不会创建,只是空白。
    • @ded-logoped 现在检查我的答案。我在我的工作室进行了测试,并确保它可以正常工作。
    【解决方案2】:

    添加此代码并清除项目并运行,希望它是显示。

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="2dp"
        android:color="@color/your_color" />
    <solid android:color="@color/your_color" /></shape> 
    

    【讨论】:

    • 您可以更改为 Imageview 并实现背景形状和 src 到图像。希望这一切正常!!!!
    【解决方案3】:

    我有一个类似的问题(它是一个按钮),我创建了一个带有角、笔触、实体等的可绘制形状,但是虽然我可以看到按钮在预览中正确显示,但我不会在预览中看到它实际应用。我还没弄清楚为什么,但解决方案是将drawable设置为按钮的背景以编程方式像这样 someButton.setBackgroundResource(R.drawable.button_rounded_corners);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-17
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      • 2018-11-03
      相关资源
      最近更新 更多