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

时间:2013-10-03 06:53:37

标签: java android

我是Android开发的新手,很难在

中设计自定义用户界面

android。我有一个默认shap的图像按钮是ractangle,我需要将其作为

舍入

请帮助我。

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#82B210"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".SettingActivity" >



<ImageButton

    android:id="@+id/imageButton1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:src="@drawable/makeconfess" />



<TextView

    android:id="@+id/textView1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_alignLeft="@+id/imageButton1"

    android:layout_alignRight="@+id/imageButton1"

    android:layout_below="@+id/imageButton1"

    android:layout_marginTop="27dp"

    android:text="  Make a\nConfess"

    android:textColor="#FFFFFF"

    android:textAppearance="?android:attr/textAppearanceMedium" />

4 个答案:

答案 0 :(得分:2)

要创建圆形按钮,首先必须在可绘制文件夹中创建一个xml

circle_shape_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" />

答案 1 :(得分:0)

xml在Android中创建一个按钮

<?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>

按钮的默认形状是矩形,所以现在我将告诉你如何创建圆形按钮

为此您还需要在drawable文件夹中创建一个xml文件,并将其命名为button_shape.xml

button_shape.xml的代码如下:

定义按钮形状的代码

<? xml version="1.0" encoding="utf-8"?>
<shape 
    xmlns:android=http://schemas.android.com/apk/res/android  android:shape="oval" > 
    //There are many other shapes also available they are rectangle,ring,line and oval
    <solid android:color="#ffcccc"/> //applying the colour to the button.
    <stroke android:width="2dp"
            android:color="#ffffff"/>

</shape>

答案 2 :(得分:0)

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

例如:

<?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个补丁绘图,如http://developer.android.com/tools/help/draw9patch.html中所述。

答案 3 :(得分:0)

在res文件夹中的drawable文件夹中

创建一个android xml文件并命名为cbutton.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>

然后将此文件设置为ImageButton的背景,如下所示。

<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>