Java 仅使用xml的圆角ImageButton?
我想做一个像这样的按钮 然而,我似乎无法找到一种仅使用xml的方法。我试过自定义库,它们似乎并不完美。没有别的办法吗?我可以做什么来代替使用ImageButton?这里是一个xml代码 1.在可绘制文件夹(如button.xml)中创建一个xml文件,并粘贴以下标记:Java 仅使用xml的圆角ImageButton?,java,android,imagebutton,rounded-corners,Java,Android,Imagebutton,Rounded Corners,我想做一个像这样的按钮 然而,我似乎无法找到一种仅使用xml的方法。我试过自定义库,它们似乎并不完美。没有别的办法吗?我可以做什么来代替使用ImageButton?这里是一个xml代码 1.在可绘制文件夹(如button.xml)中创建一个xml文件,并粘贴以下标记: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/androi
<?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.现在将此可绘制视图用作背景。如果视图是按钮,则如下所示:
<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" />
这是一个xml代码
1.在可绘制文件夹(如button.xml)中创建一个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.现在将此可绘制视图用作背景。如果视图是按钮,则如下所示:
<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" />
要在drawable中创建XML文件,请参见
<shape
android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius="28dp"
/>
<solid
android:color="#00BADE"
/>
</shape>
在drawable中创建XML文件
<shape
android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius="28dp"
/>
<solid
android:color="#00BADE"
/>
</shape>
您可以使用具有卡角半径的cardview
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"/>
并在cardview中添加imagebutton您可以使用具有卡角半径的cardview
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"/>
将imagebutton添加到cardview中该按钮上有大量文本。我想你应该使用一些布局,把所有的视图都放在里面,然后把背景设置成圆角,然后加上一个点击监听器。我不想这样,我希望图像是那样的形状。我将使用photoshop直接在图像上显示硬编码信息。那么,如果平衡发生变化,您在图像上还有另一个硬编码文本吗?是什么让你认为Photoshop和Image是答案?按钮上有很多文字。我想你应该使用一些布局,把所有的视图都放在里面,然后把背景设置成圆角,然后加上一个点击监听器。我不想这样,我希望图像是那样的形状。我将使用photoshop直接在图像上显示硬编码信息。那么,如果平衡发生变化,您在图像上还有另一个硬编码文本吗?是什么让你认为Photoshop和Image是答案?但我的要求是添加一幅图像作为背景,我该怎么做?你可以使用ImageButton替换按钮,但我的要求是添加一幅图像作为背景,我该怎么做?您可以使用ImageButton替换Button@Shashank谢蒂你可以试试这个。@Shashank谢蒂你可以试试这个。