2010-05-03 16 views
16

我有以下代碼:Android的型線

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="line"> 
    <stroke android:width="1dp"/> 
    <size android:height="1dp" /> 
    <solid android:color="#FFF"/> 
    </shape> 


    <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background ="@drawable/line"/> 

我有兩個問題:

  1. 爲什麼線是黑色的,而不是白色的?我試圖把它放在一個ImageView中,但結果是一樣的。
  2. 如何設置形狀的不透明度?

回答

21

1)爲了設置線的顏色,您需要在<stroke>上定義android:color<solid>元素用於背景。

2)您可以使用color values with an alpha layer(又名#ARGB)設置形狀的不透明度。在你的情況下,也許#7FFF。

+0

您的意思是#07FFFFFF。 – Vaiden 2014-04-27 14:15:19

+0

@Vaiden Android支持4位ARGB。在這種情況下,它將被解釋爲#77FFFFFF。 – 2014-09-02 06:10:58

8

2個黑色密度像素線的最簡單示例。 只需保存在繪製文件夾中的XML:RES /繪製/ shape_black_line.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="line"> 
<stroke 
    android:width="1dp" 
    android:color="#000000" /> 
<size android:height="2dp" /> 
</shape> 

使用的LinearLayout和背景來顯示線。

<LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/divider"> 
</LinearLayout> 

我希望這對我有所幫助。

+3

如果我們使用''而不是LinearLayout,它會不會減少內存消耗? – 2015-12-17 10:04:59

+0

是的,你可以使用它。因爲每個類都是從視圖中擴展而來的 – 2015-12-24 12:37:09