2013-08-26 42 views
7

我正在嘗試繪製DOTTED LINE.for繪製水平線,我在佈局文件中定義視圖。在我的佈局中在android佈局中創建水平虛線

 <View 
     android:layout_width="fill_parent" 
     android:layout_height="1dip" 
     android:background="@drawable/customdots" /> 

和customdots.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
    android:left="10dp" 
    android:right="10dp" 
    android:width="4dp" 
    android:drawable="@drawable/dotted" /> 

</layer-list> 

dotted.xml

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

    <size 
    android:height="4dp" 
    android:width="1024dp"/> 
    <stroke 
    android:width="4dp" 
    android:dashWidth="4dp" 
    android:color="@android:color/black" 
    android:dashGap="5dp"/> 

</shape> 

但我沒有得到使用此代碼的任何行。請幫助我。

當我使用customdots.xml在ListView分頻器作爲

android:divider="@drawable/customdots" 

它顯示了良好的虛線

+0

機器人:左= 「10dp」 機器人:右= 「10dp」 機器人:width =「4dp」 它有道理嗎? – pskink

+0

@pskink你認爲應該是什麼。 – test

+0

只有左和右? – pskink

回答

0

您可以使用下面的代碼。它可能會幫助你。 創建這樣的繪製文件夾dotted.xml ...

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="line" > 
     <stroke 
     android:color="#A52A2A" 
     android:dashWidth="10px" 
     android:dashGap="10px" /> 

</shape> 

然後用這樣的形象看你的佈局中使用這個XML ....

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This is above line of code " /> 
    <ImageView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="3dp" 
    android:layout_marginBottom="3dp" 
    android:src="@drawable/dottede" /> 
    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="this is below code " /> 
</LinearLayout> 
+1

我仍然沒有看到一條線。它顯示沒有圖像的圖像視圖。與我上面的代碼一樣的東西。 – test

+0

也一樣。也許這工作在Android的舊版本? –

+0

@SAURABH爲什麼你使用ImageView作爲分隔符而不是View? –

45

我就拉我的頭髮這個問題,直到我發現在渲染這樣的線時,Android的最新版本中存在一個錯誤。

這個錯誤可以通過在使用虛線作爲背景的視圖中添加android:layerType =「software」來規避。

實施例:

dotted.xml:

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

<stroke 
    android:dashGap="3dp" 
    android:dashWidth="8dp" 
    android:height="2px" 
    android:color="#FFFFFF" /> 

</shape> 

layout.xml:

<View 
     android:id="@+id/vDottedLine" 
     android:background="@drawable/dotted" 
     android:layout_width="match_parent" 
     android:layout_height="2px" 
     android:layerType="software" /> 
+0

謝謝..它節省了我的時間... – BSKANIA

+0

天才,謝謝。 – Lara

+2

ndroid:layerType =「software」是關鍵 –