Android 怎么设计圆角Button

来源:互联网 发布:mac版beyond compare 编辑:程序博客网 时间:2024/04/30 23:34


在res目录下的drawable-mdpi建立xml文件shape.xml,如下图所示:

注意:这里是在drawable-mdpi目录下建立,不是在layout目录下。



124626340[1].jpg 

shape.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <shape
  3.     xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:shape="rectangle">
  5.     <!-- 填充的颜色 -->
  6.     <solid android:color="#FFFFFF" />
  7.     <!-- 设置按钮的四个角为弧形 -->
  8.     <!-- android:radius 弧形的半径 -->
  9.     <corners android:radius="5dip" />
  10.     
  11. <!-- padding:Button里面的文字与Button边界的间隔 -->
  12. <padding
  13.    android:left="10dp"
  14.    android:top="10dp"
  15.    android:right="10dp"
  16.    android:bottom="10dp"
  17. />
  18. </shape>
复制代码
main.xml
在android:background="@drawable/shape"就使用了shape.xml资源

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <TextView  
  8.     android:layout_width="fill_parent" 
  9.     android:layout_height="wrap_content" 
  10.     android:text="@string/hello"
  11.     /> 
  12. <Button 
  13.         android:id="@+id/roundButton"
  14.         android:text=" 圆角按钮 "
  15.         android:layout_width="wrap_content" 
  16.     android:layout_height="wrap_content" 
  17.     android:background="@drawable/shape"
  18.         />
  19. </LinearLayout>
复制代码

strings.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <string name="hello">Hello World, RoundButtonDemoActivity!</string>
  4.     <string name="app_name">RoundButtonDemo</string>
  5. </resources>
复制代码

RoundButtonDemoActivity.java
  1. package com.android.RoundButtonDemo.activity;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. import android.widget.Toast;

  8. public class RoundButtonDemoActivity extends Activity {
  9.     Button roundButton;
  10.     @Override
  11.     public void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.main);
  14.         
  15.         roundButton=(Button)findViewById(R.id.roundButton);
  16.       //使用匿名类注册Button事件
  17.         roundButton.setOnClickListener(new OnClickListener()
  18.       {                
  19.                         public void onClick(View v)
  20.                         {
  21.                                 Toast.makeText(RoundButtonDemoActivity.this, "你点击了圆角按钮",Toast.LENGTH_LONG).show();
  22.                         }
  23.                 });
  24.     }
  25. }
复制代码

效果图:
125148263[1].jpg 
0 0
原创粉丝点击