Android中實現圓角矩形及半透明效果。

來源:互聯網
上載者:User

標籤:

註:本文由Colin撰寫,著作權!轉載請註明原文地址,謝謝合作!

在做Android開發時,我們為了美觀,有時候需要使用圓角矩形,或半透明之類的效果,在網頁設計中很容易實現。但在Android開發中,要稍微麻煩一點,但實現起來也不算很難。

關於設定背景圖片平鋪的方法請參考上一篇文章:http://itcolin.com/archives/1153.html

 

一、首先,需要在drawable-mdpi目錄裡定義一個xml檔案,我命名為frame

編寫如下代碼,其中corners 中定義每邊的圓角弧度,solid為填充的顏色:半透明顏色:#10000000~#90000000 透明深度不一樣。(也可以用:#e0000000)

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"   android:shape="rectangle" >     <solid android:color="#20000000" /><!-- <stroke android:color="#CCCCCC" android:width="1dp" android:dashWidth="5dp" android:dashGap="3dp"/> --><stroke android:color="#20000000" android:width="1dp"/>    <corners        android:bottomLeftRadius="10dp"        android:bottomRightRadius="10dp"        android:topLeftRadius="10dp"        android:topRightRadius="10dp" /></shape>

關於背景顏色需要漸層色的話也可以參考以下代碼來控制漸層:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"><gradientandroid:startColor="#FFF"android:endColor="#000"android:angle="45" /></shape>

 

二、在layout配置主檔案中將需要設定圓角的Layout(這裡演式的是RelativeLayout)背景設定為frame即可。

代碼如下:

<?xml version="1.0" encoding="utf-8"?><ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"android:id="@+id/content"android:background="@drawable/bitmap"><RelativeLayout     android:layout_width="match_parent"    android:layout_height="100dp"    android:background="@drawable/frame"    android:layout_margin="5dp"    android:padding="5dp"    android:layout_marginTop="20dp" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" /></RelativeLayout></ScrollView>

 

此時,運行虛擬機器看看效果吧。

實現效果如:

Demo : Android圓角矩形及半透明演式Demo

 

Android中實現圓角矩形及半透明效果。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.