Android開發學習筆記:圓角的Button

來源:互聯網
上載者:User

標籤:

標籤:Android Button 休閑 圓角 移動開發 

在res目錄下的drawable-mdpi建立xml檔案shape.xml,如所示:

 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.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.Toast;  
  9.  
  10. public class RoundButtonDemoActivity extends Activity {  
  11.     Button roundButton;  
  12.     @Override 
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.           
  17.         roundButton=(Button)findViewById(R.id.roundButton);  
  18.       //使用匿名類註冊Button事件  
  19.         roundButton.setOnClickListener(new OnClickListener()  
  20.       {       
  21.             public void onClick(View v)  
  22.             {  
  23.                 Toast.makeText(RoundButtonDemoActivity.this, "你點擊了圓角按鈕",Toast.LENGTH_LONG).show();  
  24.             }  
  25.         });  
  26.     }  

 

 

 

本文出自 “IT的點點滴滴” 部落格,請務必保留此出處http://liangruijun.blog.51cto.com/3061169/630051

Android開發學習筆記:圓角的Button

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.