1. 程式人生 > >Android自定義Shape使EditText化身為Bootstrap 風格的 textarea (樣式之Shape)

Android自定義Shape使EditText化身為Bootstrap 風格的 textarea (樣式之Shape)

Android樣式開發之Shape篇:學習Shape的應用

一:案例1:自定義Shape使EditText化身為Bootstrap 風格的 textarea

1.在 drawable 中 shape xml檔案

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#FFFFFF"/>

    <stroke
android:width="0.5dp" android:color="#dddddd"/>
<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"/> <corners android:radius="10dp"/> </shape>

2.通過background引用定義好的shape

<EditText
       android:id="@+
id/qj_content"
android:layout_below="@id/divider_three" android:layout_width="match_parent" android:layout_height="100dp" android:enabled="true" android:gravity="top" android:minLines="8" android:maxLines="10" android:scrollbars="vertical" android:background="@drawable
/shape_textarea"
android:hint="請假內容" android:textColorHint="@color/hint_text_gray"/>

3.效果圖:

這裡寫圖片描述

二:看看shape的基本用法和常見屬性

1.把shape定義的xml檔案存放在drawable目錄下

2.用通過android:shape屬性可以指定:shape的形狀

  • rectangle: 矩形,預設的形狀,可以畫出直角矩形、圓角矩形、弧形等
  • oval: 橢圓形,用得比較多的是畫正圓
  • line: 線形,可以畫實線和虛線
  • ring: 環形,可以畫環形進度條

常見用法如下:

<?xml version="1.0" encoding="utf-8"?>  
<!-- android:shape指定形狀型別,預設為rectangle -->  
<shape xmlns:android="http://schemas.android.com/apk/res/android"  
    android:shape="rectangle">  

    <!-- solid指定形狀的填充色,只有android:color一個屬性 -->  
    <solid android:color="#2F90BD" />  

    <!-- padding設定內容區域離邊界的間距 -->  
    <padding  
        android:bottom="12dp"  
        android:left="12dp"  
        android:right="12dp"  
        android:top="12dp" />  

    <!-- corners設定圓角,只適用於rectangle -->  
    <corners android:radius="10dp" />  

    <!-- stroke設定描邊 -->  
    <stroke  
        android:width="2dp"  
        android:color="@android:color/darker_gray"  
        android:dashGap="4dp"  
        android:dashWidth="4dp" />  
</shape>  

3.shape有6個屬性,分別為:

  • solid:填充物,只有android:color一個屬性
  • corners:設定圓角,即四個角的弧度
  • gradient:顏色漸變
  • padding:間隔,xml裡經常用到,不比多解釋
  • size:長寬
  • stroke:設定圖片邊緣顏色
  1. solid: 設定形狀填充的顏色,只有android:color一個屬性
  2. padding: 設定內容與形狀邊界的內間距,可分別設定左右上下的距離
    • android:left 左內間距
    • android:right 右內間距
    • android:top 上內間距
    • android:bottom 下內間距
  3. gradient: 設定形狀的漸變顏色,可以是線性漸變、輻射漸變、掃描性漸變
  4. android:type 漸變的型別
    • linear 線性漸變,預設的漸變型別
    • radial 放射漸變,設定該項時,android:gradientRadius也必須設定
    • sweep 掃描性漸變
    • android:startColor 漸變開始的顏色
    • android:endColor 漸變結束的顏色
    • android:centerColor 漸變中間的顏色
    • android:angle 漸變的角度,線性漸變時才有效,必須是45的倍數,0表示從左到右,90表示從下到上
    • android:centerX 漸變中心的相對X座標,放射漸變時才有效,在0.0到1.0之間,預設為0.5,表示在正中間
    • android:centerY 漸變中心的相對X座標,放射漸變時才有效,在0.0到1.0之間,預設為0.5,表示在正中間
    • android:gradientRadius 漸變的半徑,只有漸變型別為radial時才使用
    • android:useLevel 如果為true,則可在LevelListDrawable中使用
  5. corners: 設定圓角,只適用於rectangle型別,可分別設定四個角不同半徑的圓角,當設定的圓角半徑很大時,比如200dp,就可變成弧形邊了
    • android:radius 圓角半徑,會被下面每個特定的圓角屬性重寫
    • android:topLeftRadius 左上角的半徑
    • android:topRightRadius 右上角的半徑
    • android:bottomLeftRadius 左下角的半徑
    • android:bottomRightRadius 右下角的半徑
  6. stroke: 設定描邊,可描成實線或虛線。
    • android:color 描邊的顏色
    • android:width 描邊的寬度
    • android:dashWidth 設定虛線時的橫線長度
    • android:dashGap 設定虛線時的橫線之間的距離

三:案例2:使用Shape簡單自定義button

1.在drawable中定義shape_submit_btn.xml

<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android"  
    android:shape="rectangle">  

    <solid android:color="#99b638"/>  

    <corners android:radius="10dp"/>  

</shape>  

2.用background引用shape_submit_btn.xml

<Button  
       android:id="@+id/btn_submit_qjsb"  
       android:layout_width="match_parent"  
       android:layout_height="40dp"  
       android:layout_margin="10dp"  
       android:background="@drawable/shape_submit_btn"  
       android:textColor="@color/white"  
       android:text="提交"  
       />  

3.效果圖:

這裡寫圖片描述

以上只是Shape的簡單用法,活用Shape還可以實現簡便等其他更酷的效果哦

相關推薦

Android定義Shape使EditText身為Bootstrap 風格textarea (樣式Shape)

Android樣式開發之Shape篇:學習Shape的應用 一:案例1:自定義Shape使EditText化身為Bootstrap 風格的 textarea 1.在 drawable 中 shape xml檔案 <?xml version="

Android 定義多行EditText,監聽最多輸入字元數字顯示

帶數字指示的EditText 定義所需屬性attrs.xml <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="LinesEditVi

Android定義控制元件-EditText(可用於登陸介面)

最近在研究前輩寫的程式碼,看到了有關於登陸介面的使用者名稱和密碼,使用的是自定義EditText的,所以寫兩篇相關文章來記錄。 其實使用者名稱和密碼使用的EditText控制元件非常相似,拿使用者名稱處使用的控制元件為例,它包括如下功能: 在沒內容

android-定義組合控制元件(EditText+選項)

一.前言 在開發中,或許一個業務需求中會出現很多系統控制元件組合成的佈局,並且經常需要複用。比如在一個表單中,裡面有個編輯框EditText右側還需要有個選項卡按鈕,需要有編輯框的輸入功能也需要有右側選項卡的點選事件,同時這兩個控制元件也存在一定關聯,且在一個介

Android定義view - shape

空心圓 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="o

android定義View定義EditText(新增刪除功能)

           忙忙碌碌20天,新的專案終於接近尾聲了。今天公司召集幾個使用者體驗師和美工一起吐糟這20天做的這個新產品,對於產品提出了很多建議,這幾天就改介面了。在這個專案中大量的使用了EditText元件,並且添加了刪除功能。這裡面都是用RelativeLayou

Android 定義帶刪除按鈕的EditText

首先建立一個類 設定成EditTextWithDel,繼承EditText。 實現程式碼如下。 @SuppressLint("AppCompatCustomView") public class EditTextWithDel extends EditText { priv

Android定義EditText——帶一鍵清除和密碼明文切換按鈕,支援多樣式選擇

      Android自定義View開始入坑,打算寫一些自定義控制元件練練手。       這是一個自定義EditText,帶一鍵清除和密碼明文切換按鈕(可以傳入自定義圖片資源),可以自定義邊框顏色,還支援四種邊框樣式的選擇。       原始碼已上傳 GitHub: 

Android定義ViewGroup使每行元件數量不確定,並拿到選中資料

1先看效果圖 2專案目錄 3在定義控制元件FlowTagGroup package android.zhh.com.myviewgroup; /** * Created by sky on 2017/3/10. */ import android.conten

Android 定義Dialog 裡面有EditText控制元件點選關閉輸入法失效的問題

在做一個專案的時候,自定義額了一個Dialog 裡面有取消,儲存按鈕,以及EditText,在輸入法彈出的時候,點選取消按鈕,自定義dialog對話方塊關閉了,但是輸入法始終關閉不了,在網上找了很多方法,都不能很好的解決,輸入法不能關閉的問題。    最好找到的解決辦法是

Android定義EditText:手把手教你做一款含一鍵刪除&定義樣式的SuperEditText

前言 Android開發中,EditText的使用 非常常見 本文將手把手教你做一款 附帶一鍵刪除功能 & 自定義樣式豐富的 SuperEditText控制元件,希望你們會喜歡。 目錄 1. 簡介 一款 附帶一鍵刪除功

Android 定義評價Dialog(RatingBar的使用和EditText的剩餘字數顯示)

先看一下效果圖 主要介紹一下 五角星評分控制元件(RatingBar) 和 EditText下方剩餘字數的顯示 RatingBar的使用 <RatingBar             andr

Android定義方框EditText註冊驗證碼

先來個效果圖讓大家看一看,現在好多app都用類似的註冊頁 實現思路 用一個透明的EditText與四個TextView重疊,並給TextView設定預設背景 第4個TextView輸入完成後,要設定回撥,並且要加入增加刪除的回撥 還要監聽EditTe

Android定義EditText,實現分割輸入內容效果

例如,輸入一組25位的序列號,希望分成5組,每組5個數字。效果如下圖。 Android中沒有發現類似的空間,只好自己動手了。 1.首先自定義控制元件繼承EditText,實現相應方法。  Java程式碼   package com.example.sequencenu

Android 定義view-仿新浪微博#話題#插入EditText

不小心開啟新浪微博發微博頁面有個可以插入話題#…#的功能,看著挺好玩的。就照著實現了一下。 如果不知道怎麼樣效果的可以開啟微博看看。大概的功能是: 插入話題使用#特殊符號開頭和結尾 話題文字高亮顯示 刪除話題選中整個話題文字一次性刪除 可以插入多個話題

Android定義Dialog,實現性別選擇,日期選擇,獲取EditText內容

Android 自定義 Dialog ,實現 性別選擇,日期選擇和 自定義佈局獲取EditText內容 Dialog類是對話方塊的基類,但是應該避免直接例項化Dialog,而是應該儘量使用下列子列之一 : AlertDialog 此對話方塊可

Android定義控制元件並且使其可以在xml中定義屬性

package org.xiaom.customView.view; import org.xiaom.customView.R; public class MyView extends LinearLayout { private View root = null; // 上面的img priva

Android定義控制元件:如何使view動起來?

本文發表於CSDN《程式設計師》 摘要 Android中的很多控制元件都有滑動功能,但是很多時候原生控制元件滿足不了需求時,就需要自定義控制元件,那麼如何能讓控制元件滑動起來呢?本文主要總結幾種可以使控制元件滑動起來的方法 實現 其實能讓view

Android 定義控件繼承ViewGroup創建新容器

多個 osi count() arc ron min ole tro 定位 歡迎大家來學習本節內容,前幾節我們已經學習了其他幾種自定義控件,分別是Andriod 自定義控件之音頻條及 Andriod 自定義控件之創建可以復用的組合控件還沒有學習的同學請先去學習下,因為本節將

Android定義相機超詳細講解

了解 catch 實現 4.4 required form 需要 eset 自己 Android自定義相機超詳細講解 轉載請標明出處: http://blog.csdn.net/vinicolor/article/details/49642861; 由於網上關於Andr