2012-03-28 226 views
38

行我使用的是可繪製作爲一個TextView的背景正好有文字下方的分隔線。一個與此繪製的XML achivied它:形狀繪製爲背景,在底部

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item> 
     <shape 
     android:shape="rectangle"> 
      <solid android:color="#FFDDDDDD" /> 
      <gradient 
       android:angle="0" 
       android:startColor="#FFAAAAAA" 
       android:endColor="#FFEEEEEE" 
       /> 
     </shape> 
    </item> 

    <item android:bottom="2dp"> 
     <shape 
     android:shape="rectangle"> 
      <solid android:color="#FF000000" /> 
     </shape> 
    </item> 

</layer-list> 

但這種方法繪製上面一個黑色的矩形彩色矩形。我想只在沒有黑色矩形的形狀底部劃線,因爲黑色不透明。我怎麼能做到這一點?

+0

好像矯枉過正。爲什麼不使用彩色視圖? – dmon 2012-03-28 21:19:38

+0

同樣適用 - 我將如何在該視圖的底部添加細線? – ZoolWay 2012-03-28 21:29:58

+2

這就是我的意思,視圖是線。 '<查看機器人:的backgroundColor =「#FF000000」機器人:layout_height =「2DP」機器人:layout_width =「FILL_PARENT」 />' – dmon 2012-03-28 21:56:43

回答

27

一般情況下,我嘗試亂七八糟儘量少與背景,除非絕對必要,因爲這樣做將覆蓋有狀態的默認背景顏色集中,壓,等我建議只使用一個額外的視圖(垂直LinearLayout),它和你需要的一樣厚。例如:

<View 
     android:background="#FF000000" 
     android:layout_height="2dp" 
     android:layout_width="fill_parent"/> 
+7

嗯,我不是很相信你的解決方案,因爲它需要額外的佈局和額外的觀點,都是重對象只是一個水平線...如果你們對背景過敏,爲什麼不重寫onDraw()並畫出一條線? – rupps 2013-05-08 18:03:44

+4

這是骯髒的解決方法 – ruX 2014-01-27 14:58:38

+0

ahahahahaha很簡單,很明顯。我嘗試過沒有成功做一個簡單的底線背景。爲什麼我不這麼認爲?謝謝 – pablobaldez 2014-12-30 20:17:51

158

這就是我如何在底部得到一條線。繪製筆畫,但再向上轉移項目和兩側,以獲得頂部與側面以不顯示行程:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:top="-6dp" android:left="-6dp" android:right="-6dp"> 
     <shape> 
      <solid android:color="#2b7996"/> 
      <stroke android:color="#33b5e5" android:width="6dp"/> 
     </shape> 
    </item> 
</layer-list> 
+14

這應該是IMO接受的答案,因爲它可以直接解決問題。 – Abhishek 2014-09-01 10:44:28

+0

我聽起來像OP希望避免在實體區域出現顏色。情況就是如此。使用#00000000,實體可以是透明的,而不是#2b7996。一個8位十六進制值指定前兩位數字的字母。 – 2015-09-18 19:35:30

+0

輝煌的解決方法:) – eMich 2015-09-24 21:25:54

9

我認爲這是更好的解決方案:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:gravity="bottom"> 
     <shape> 
      <size android:height="1dp" /> 
      <solid android:color="#000000" /> 
     </shape> 
    </item> 
</layer-list> 
+0

該解決方案几乎適用於除三星棒棒糖外的所有設備。整個視圖用「solid」標籤中提供的顏色繪製。由於三星的市場份額,這不應該被忽略。 – 2017-05-18 12:35:58

1

有了這個解決方案,其中過你需要不同的路線。我的要求只是強調。即使你可以給佈局以不同的顏色。你可以看到下面的圖片,白線enter image description here

<?xml version="1.0" encoding="UTF-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 

<item android:top="-5dp" android:left="-5dp" android:right="-5dp"> 
    <shape> 
     <solid android:color="@android:color/transparent"/> 
     <stroke android:color="@color/white" android:width="5dp"/> 
    </shape> 
</item> 

<item android:top="-5dp" android:bottom="-5dp" android:right="-5dp"> 
    <shape> 
     <solid android:color="@android:color/transparent"/> 
     <stroke android:color="@color/colorPrimaryDark" android:width="5dp"/> 
    </shape> 
</item> 

<item android:bottom="-5dp" android:left="-5dp" android:right="-5dp"> 
    <shape> 
     <solid android:color="@android:color/transparent"/> 
     <stroke android:color="@color/colorPrimaryDark" android:width="5dp"/> 
    </shape> 
</item> 

<item android:top="-5dp" android:left="-5dp" android:bottom="-5dp"> 
    <shape> 
     <solid android:color="@android:color/transparent"/> 
     <stroke android:color="@color/colorPrimaryDark" android:width="5dp"/> 
    </shape> 
</item> 

<item android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="5dp"> 
    <shape> 
     <solid android:color="@color/colorPrimary"/> 

     <corners android:bottomRightRadius="0dp" 
      android:bottomLeftRadius="0dp" 
      android:topLeftRadius="0dp" 
      android:topRightRadius="0dp" /> 
    </shape> 

</item> 

2

通常對於類似的任務 - 我創建的圖層列表繪製像這樣的:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item> 
    <shape android:shape="rectangle"> 
     <solid android:color="@color/underlineColor"/> 
    </shape> 
</item> 
<item android:bottom="3dp"> 
    <shape android:shape="rectangle"> 
     <solid android:color="@color/buttonColor"/> 
    </shape> 
</item> 

的想法是首先你用underlineColor繪製矩形,然後在這個矩形的頂部用另一個實際的buttonColor繪製矩形,但應用bottomPadding。它始終有效。

但是,當我需要有buttonColor是透明的,我可以不使用上述繪製。我發現多了一個解決方案

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item> 
    <shape android:shape="rectangle"> 
     <solid android:color="@android:color/transparent"/> 
    </shape> 
</item> 

<item android:drawable="@drawable/white_box" android:gravity="bottom" android:height="2dp"/> 
</layer-list> 

(你可以在這裏看到mainButtonColor是透明的,white_box僅僅是一個簡單的矩形繪製與白色固體)

+0

'android:gravity =「bottom」'需要min API 23工作。 – 2017-11-20 08:04:55