如何在android中制作自定义按钮

时间:2013-11-04 15:47:01

标签: android

我想制作自定义按钮看起来像这些

enter image description here

我搜索了很多但是无法成功。我该怎么做!我想在xml中制作,而不是在Photoshop中制作。

3 个答案:

答案 0 :(得分:0)

学习制作自定义可绘制的xml资源。 以下是设置按钮,以便在一分钟内看起来很好看 可能你可以从他们的代码中获得一些想法!

http://www.mindfreakerstuff.com/2012/09/50-useful-android-custom-button-style-set-1/

答案 1 :(得分:0)

达到你想要做的事情的方法是为按钮创建两个图像,一个用于未按下的按钮,一个用于按下按钮,相对称为“按钮”和“按钮”,并将它们放在res / drawable文件夹中。这些一起需要以下名为“selector.xml”的xml文件:

<item
    android:state_pressed="true"
    android:drawable="@drawable/buttondown" /> <!-- pressed -->

<item
    android:state_focused="true"
    android:drawable="@drawable/buttonup" /> <!-- focused -->

<item
    android:drawable="@drawable/buttonup" /> <!-- default -->

现在,您可以在按钮标记中的布局xml文件中实现所有添加此代码:

机器人:背景= “@绘制/选择”

答案 2 :(得分:0)

在Android 4.3上,您可以使用 ImageButton

on xml

    <ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/android_button"
    android:onClick="doSomthing" />

代码

public class MyAndroidAppActivity extends Activity 
{

 ImageButton imageButton;

 @Override
 public void onCreate(Bundle savedInstanceState) 
 {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     imageButton = (ImageButton) findViewById(R.id.imageButton1);
 }

 public void doSomthing(View v)
 {
     //what happan when button pressed
 }
}


 机器人:SRC = “@绘制/ android_button”

“android_button”是res / drawable中的图像名称