Android开发学习笔记:圆角的Button
在res目录下的drawable-mdpi建立xml文件shape.xml,如下图所示:
shape.xml<?xmlversion="1.0"encoding="UTF-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--填充的颜色--><solidandroid:color="#FFFFFF"/><!--设置按钮的四个角为弧形--><!--android:radius弧形的半径--><cornersandroid:radius="5dip"/><!--padding:Button里面的文字与Button边界的间隔--><paddingandroid:left="10dp"android:top="10dp"android:right="10dp"android:bottom="10dp"/></shape>
main.xml在android:background="@drawable/shape"就使用了shape.xml资源
<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/><Buttonandroid:id="@+id/roundButton"android:text="圆角按钮"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/shape"/></LinearLayout>
strings.xml<?xmlversion="1.0"encoding="utf-8"?><resources><stringname="hello">HelloWorld,RoundButtonDemoActivity!</string><stringname="app_name">RoundButtonDemo</string></resources>RoundButtonDemoActivity.java
packagecom.android.RoundButtonDemo.activity;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.Toast;publicclassRoundButtonDemoActivityextendsActivity{ButtonroundButton;@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);roundButton=(Button)findViewById(R.id.roundButton);//使用匿名类注册Button事件roundButton.setOnClickListener(newOnClickListener(){publicvoidonClick(Viewv){Toast.makeText(RoundButtonDemoActivity.this,"你点击了圆角按钮",Toast.LENGTH_LONG).show();}});}}
效果图:声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。