Android开发圆角button,Android开发学习笔记:圆角的Button

本文介绍如何在Android中通过在res/drawable-mdpi目录下创建shape.xml来定义一个矩形形状,并设置边距,从而实现圆角按钮的效果。在main.xml中,将shape.xml作为背景应用到按钮上,同时展示了相应的布局和代码实现。当点击按钮时,会显示一个Toast提示。
摘要由CSDN通过智能技术生成

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

13f0271fe816a5783ba71605f01ad3df.png

shape.xml

fcecaa27ea5212ceb9bf034c36bfbf34.gif<?xml  version="1.0" encoding="UTF-8"?> 

xmlns:android="http://schemas.android.com/apk/res/android"

android:shape="rectangle">

 

android:left="10dp"

android:top="10dp"

android:right="10dp"

android:bottom="10dp" /> 

fcecaa27ea5212ceb9bf034c36bfbf34.gif

main.xml

在android:background="@drawable/shape"就使用了shape.xml资源

fcecaa27ea5212ceb9bf034c36bfbf34.gif<?xml  version="1.0" encoding="utf-8"?> 

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>   

android:id="@+id/roundButton"

android:text=" 圆角按钮 "

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/shape"

/> 

fcecaa27ea5212ceb9bf034c36bfbf34.gif

strings.xml<?xml  version="1.0" encoding="utf-8"?> 

Hello World, RoundButtonDemoActivity!

RoundButtonDemo 

RoundButtonDemoActivity.java

fcecaa27ea5212ceb9bf034c36bfbf34.gifpackage 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();

}

});

}

}

fcecaa27ea5212ceb9bf034c36bfbf34.gif

效果图:

b83d081a24383b8c510915cd2f1edb6a.png

原文:http://8988940.blog.51cto.com/8978940/1577508

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值