圆角的按钮实现扁平化的UI很有美感,但是实现起来也不算太难。
在res目录下的drawable-mdpi建立xml文件shape.xml,如下图所示:
shape.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充的颜色 -->
<solid android:color="#FFFFFF" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="5dip" />
<!-- padding:Button里面的文字与Button边界的间隔 -->
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"
/>
</shape>
main.xml
在android:background="@drawable/shape"就使用了shape.xml资源
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/roundButton"
android:text=" 圆角按钮 "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape"
/>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, RoundButtonDemoActivity!</string>
<string name="app_name">RoundButtonDemo</string>
</resources>
RoundButtonDemoActivity.java
package com.android.RoundButtonDemo.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class RoundButtonDemoActivity extends Activity {
Button roundButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
roundButton=(Button)findViewById(R.id.roundButton);
//使用匿名类注册Button事件
roundButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(RoundButtonDemoActivity.this, "你点击了圆角按钮",Toast.LENGTH_LONG).show();
}
});
}
}
效果图:
分享到:
相关推荐
Android 通用圆角布局,快速实现圆角需求。 之前做项目的时候有圆角相关需求,在网上找了很多方案都不够满意,于是自己做了一个,目前已经使用了一段时间,更新了多个版本,我遇到的问题都进行了修复,并且添加了很...
可设置圆角背景边框的的按钮 通过调节色彩明度自动计算按下(pressed)状态颜色 Gradle repositories { maven { url "https://jitpack.io" } } dependencies { compile 'com.github.czy1121:roundbutton:1.1.0' ...
delphi制作的圆角按钮,使按钮的边角变成圆形
android中对Bitmap图片设置任意角为圆角
Android Studio 在xml文件中设置界面布局1、xml文件中给输入框editText修改下划线颜色2、设置button按钮为圆角及修改按钮颜色3、运用约束布局时预览效果和运行效果不一致 写一写今天困扰我很久的问题:如何给...
android ImageView网络图片加载、动态设置尺寸、圆角(绝对好用)
自定义的button样式,圆角按钮,圆角button
Android 自定义imageview实现图片圆角,圆形图片,椭圆形图片,不需要添加依赖,简洁实现
Android设置虚线、圆角、渐变,详情请链接至: http://blog.csdn.net/lan410812571/article/details/9946991
安卓Android源码——安卓Android创建抗锯齿透明背景圆角图像.rar
安卓Android源码——安卓Android创建抗锯齿透明背景圆角图像.zip
Android常用实例——实现修改用户头像功能.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
在xml布局文件中实现自定义圆角布局
圆角按钮宽度自适应 自适应宽度的CSS圆角背景的按钮
Qt圆角功能和状态组合按钮,可以显示颜色或者图片
Android圆角进度显示 Android圆角进度显示 Android圆角进度显示 Android圆角进度显示 Android圆角进度显示
首先,在res下面新建一个文件夹drawable,在drawable下面新建三个xml文件:shape_corner_down.xml、shape_corner_up.xml和shape_corner.xml,分别是下面两个角是圆角边框,上面两个角是圆角边框,四个角全部是圆角...
使用此资源,您可以轻松创建许多形式的Unity ui按钮图片等面板,赶快试试吧! 优点: - 面板使用网格进行显示,速度很快 - 可自定义的段数 - 可自定义的内部宽度 / 外部宽度 - 任何RGBA颜色 - 相对/绝对宽度 ...
自定义纯色圆角按钮样式,不用图片也可以实现纯色按钮的效果!
安卓Android源码——创建抗锯齿透明背景圆角图像.zip