Java 如何在android中自定义图像按钮

Java 如何在android中自定义图像按钮,java,android,Java,Android,我是android开发新手,很难在android中设计定制的用户界面 安卓。我有一个图像按钮,有默认的形状是我需要做的角度 圆的 请帮帮我 xmlns:tools=”http://schemas.android.com/tools" android:layout\u width=“匹配父项” android:layout\u height=“match\u parent” android:background=“#82B210” android:paddingBottom=“@dimen/ac

我是android开发新手,很难在android中设计定制的用户界面

安卓。我有一个图像按钮,有默认的形状是我需要做的角度

圆的

请帮帮我

xmlns:tools=”http://schemas.android.com/tools"
android:layout\u width=“匹配父项”
android:layout\u height=“match\u parent”
android:background=“#82B210”
android:paddingBottom=“@dimen/activity\u vertical\u margin”
android:paddingLeft=“@dimen/activity\u水平\u边距”
android:paddingRight=“@dimen/activity\u水平\u边距”
android:paddingTop=“@dimen/activity\u vertical\u margin”
工具:上下文=“.SettingActivity”>

在Android中创建按钮的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:background="@drawable/one" />    

<! --  android:background property is  used to apply background to a to button. And @drawable/one means there is a image name as one in your drawable folder we are applying it as background to this button. -->

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

定义圆角按钮的代码

<? xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">   //Defines the shape of button you wants
    <solid android:color="#eeffff"/>
    <corners android:bottomLeftRadius="8dp"  //how much curve you want from bottom left corner
             android:bottomRightRadius="8dp"//how much curve you want from bottom right corner
             android:topLeftRadius="8dp""//how much curve you want from top left corner
             android:topRightRadius="8dp"/> "//how much curve you want from top right corner

</shape>

//定义所需按钮的形状

您可以使用xml文件定义
按钮
图像按钮
,并将其保存到
可绘制的
文件夹中。通过这种方式,您可以为按钮提供不同的状态(按下、聚焦、正常等)

例如:

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/btn_action_hover" />
    <item android:state_selected="true" android:drawable="@drawable/btn_action_hover" />
    <item android:state_focused="true" android:drawable="@drawable/btn_action_hover" />
    <item android:drawable="@drawable/btn_action_normal" />
</selector>


为了确保您的按钮在任何分辨率/屏幕大小上都能正确显示,我建议使用9个补丁可绘制工具,如中所述。

在res文件夹的可绘制文件夹中

创建一个名为cbutton.xml的android xml文件

然后输入下面的代码

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:state_pressed="true" 
          android:drawable="@drawable/pressselected">
    </item> 
    <item android:state_focused="true" 
          android:drawable="@drawable/presshighlight"> 
    </item> 
    <item  android:drawable="@drawable/press"></item> 
</selector>

要创建按钮的圆形,首先必须在drawable文件夹中创建xml

circle\u shape\u drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">
    <solid 
       android:color="#666666"/>
   <size 
       android:width="120dp"
        android:height="120dp"/>
</shape>

在按钮背景中使用此xml作为

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circle_shape_drawable"
        android:text="@string/hello_world" />


我需要按钮的形状是圆形或圆形而不是矩形,所以我应该怎么做?希望你得到你想要的:)我想添加背景图像如何做?你想要圆形图像作为矩形按钮的背景吗?
<LinearLayout xmlns:android = http://schemas.android.com/apk/res/android 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" >

<TextView 
android:id="@+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:layout_marginTop="10dp" 
android:text="Click to view Custom Button Action" 
android:textAppearance="?android:attr/textAppearanceMedium" />

<Button 
android:id="@+id/button1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background="@drawable/press" 
android:layout_gravity="center" 
android:layout_marginTop="30dp" 
/>

   </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">
    <solid 
       android:color="#666666"/>
   <size 
       android:width="120dp"
        android:height="120dp"/>
</shape>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circle_shape_drawable"
        android:text="@string/hello_world" />