1

我是android开发的新手,设计自定义用户界面非常困难

安卓 。我有一个默认形状为 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

4 回答 4

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" />
于 2013-10-03T07:27:48.337 回答
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>

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

为此,您再次需要在可绘制文件夹中创建一个 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>
于 2013-10-03T06:57:57.393 回答
0

您可以使用 xml 文件定义ButtonorImageButton并将其保存到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中所述。

于 2013-10-03T07:05:21.993 回答
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>
于 2013-10-03T07:11:30.090 回答