歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發--圓角按鈕和繪制直線的實現

Android開發--圓角按鈕和繪制直線的實現

日期:2017/3/1 10:02:22   编辑:Linux編程

我們通常會覺得系統給出的按鈕不夠美觀,這時,我們可以自己定義一個按鈕,已達到自己的需求,在這裡實現一個圓角按鈕。

需要在drawable文件夾下新建一個xml文件,並以shape為根標簽,利用如下的shape即可實現需求:

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:Android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充的顏色(白色) -->
<solid android:color="#0000FF" />
<!-- 設置按鈕的四個角為弧形 -->
<!-- android:radius 弧形的半徑 -->
<corners android:radius="5dip" />

<!-- padding:Button裡面的文字與Button邊界的間隔 -->
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"
/>
</shape>

同時需要在layout文件裡為Button標簽添加如下一行代碼進行聲明:

android:background="@drawable/shape"

下面,我想在Activity中繪制一條直線,實現也很簡單,只需要使用View作為根標簽,同時設置高度為1px即可,下面給出實現的聲明:

<View
android:layout_height="1px"
android:background="#ff0000"
android:layout_width="fill_parent">
</View>

下圖是實現的截圖:

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved