----------------------------------View虛線或者直線(源代碼下有屬性解釋)-----------------------------------------------------
一、shape 樣式:(在drawable新建--》new--》Drawable resource file 並把原父級標簽selector改為shape )
<!--只能畫水平線,畫不了豎線;-->
<!--線的高度是通過stroke的android:width屬性設置的;-->
<!--size的android:height屬性定義的是整個形狀區域的高度;-->
<!--size的height必須大於stroke的width,否則,線無法顯示;-->
<!--線在整個形狀區域中是居中顯示的;-->
<!--線左右兩邊會留有空白間距,線越粗,空白越大;-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:width="5dp"
android:color="#4ec5ff"
android:dashGap="2dp"
android:dashWidth="10dp" />
</shape>
二、style 樣式:
<style name="line">
<item name="android:background">@drawable/buttonlinestyle</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">40dp</item>
</style>
三、Button控件調用style樣式:
<!--引用虛線的view需要添加屬性android:layerType,值設為"software",否則顯示不了虛線。-->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ly.blogtest.MainActivity">
<LinearLayout
style="@style/line"
android:id="@+id/linearLayout"
android:orientation="horizontal"
android:layerType="software"
>
</LinearLayout>
</RelativeLayout>
----------------------------------View虛線或者直線-----------------------------------------------------
---------------------------------畫水平線、虛線注意-----------------------------------------------
- 只能畫水平線,畫不了豎線;
- 線的高度是通過stroke的android:width屬性設置的;
- size的android:height屬性定義的是整個形狀區域的高度;
- size的height必須大於stroke的width,否則,線無法顯示;
- 線在整個形狀區域中是居中顯示的;
- 線左右兩邊會留有空白間距,線越粗,空白越大;
- 引用虛線的view需要添加屬性android:layerType,值設為"software",否則顯示不了虛線。
---------------------------------划線注意-----------------------------------------------
----------------------------------android:shape屬性指定形狀------------------------------
- rectangle: 矩形,默認的形狀,可以畫出直角矩形、圓角矩形、弧形等
- oval: 橢圓形,用得比較多的是畫正圓
- line: 線形,可以畫實線和虛線
- ring: 環形,可以畫環形進度條
----------------------------------android:shape屬性指定形狀------------------------------
----------------------------------shape的屬性標簽-----------------------------------------
<shape>
<!-- 實心 -->
<solid android:color="#ff9d77"/>
<!-- 漸變 -->
<gradient
android:startColor="#ff8c00"
android:endColor="#FFFFFF"
android:angle="270" />
<!-- 描邊 -->
<stroke
android:width="2dp"
android:color="#dcdcdc" />
<!-- 圓角 -->
<corners
android:radius="2dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
-
solid: 設置形狀填充的顏色,只有android:color一個屬性
- android:color 填充的顏色
-
padding: 設置內容與形狀邊界的內間距,可分別設置左右上下的距離
- android:left 左內間距
- android:right 右內間距
- android:top 上內間距
- android:bottom 下內間距
-
gradient: 設置形狀的漸變顏色,可以是線性漸變、輻射漸變、掃描性漸變
- 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中使用
- android:type 漸變的類型
-
corners: 設置圓角,只適用於rectangle類型,可分別設置四個角不同半徑的圓角,當設置的圓角半徑很大時,比如200dp,就可變成弧形邊了
- android:radius 圓角半徑,會被下面每個特定的圓角屬性重寫
- android:topLeftRadius 左上角的半徑
- android:topRightRadius 右上角的半徑
- android:bottomLeftRadius 左下角的半徑
- android:bottomRightRadius 右下角的半徑
-
stroke: 設置描邊,可描成實線或虛線。
- android:color 描邊的顏色
- android:width 描邊的寬度
- android:dashWidth 設置虛線時的橫線長度
- android:dashGap 設置虛線時的橫線之間的距離
----------------------------------shape的屬性標簽-----------------------------------------