仅使用 xml 的带圆角的 ImageButton?
ImageButton with rounded corner using xml only?
我正在尝试制作这样的 ImageButton
但是,我似乎找不到仅使用 xml 的方法。我尝试了自定义库,它们似乎无法完美运行。没有别的办法吗?我可以做什么而不是使用 ImageButton?
这是一个 xml 代码。
1.Create 在您的可绘制文件夹中创建一个 xml 文件,例如 button.xml 并粘贴以下标记:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="3dp" />
<stroke android:width="1dp" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<solid android:color="#58857e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />
</shape>
</item>
</selector>
2.Now 使用此可绘制对象作为视图的背景。如果视图是按钮那么像这样:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#ffffff"
android:background="@drawable/mybutton"
android:text="Buttons" />
感谢this answer
在 drawable 中创建一个 XML 文件。
<shape
android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius="28dp"
/>
<solid
android:color="#00BADE"
/>
</shape>
然后在您的 imageButton 或其他任何内容中。
android:background="@drawable/buttonShape"
您可以使用带有卡片角半径的卡片视图
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"/>
并在 cardview 中添加图像按钮
我正在尝试制作这样的 ImageButton
但是,我似乎找不到仅使用 xml 的方法。我尝试了自定义库,它们似乎无法完美运行。没有别的办法吗?我可以做什么而不是使用 ImageButton?
这是一个 xml 代码。
1.Create 在您的可绘制文件夹中创建一个 xml 文件,例如 button.xml 并粘贴以下标记:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="3dp" />
<stroke android:width="1dp" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<solid android:color="#58857e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />
</shape>
</item>
</selector>
2.Now 使用此可绘制对象作为视图的背景。如果视图是按钮那么像这样:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#ffffff"
android:background="@drawable/mybutton"
android:text="Buttons" />
感谢this answer
在 drawable 中创建一个 XML 文件。
<shape
android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius="28dp"
/>
<solid
android:color="#00BADE"
/>
</shape>
然后在您的 imageButton 或其他任何内容中。
android:background="@drawable/buttonShape"
您可以使用带有卡片角半径的卡片视图
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"/>
并在 cardview 中添加图像按钮