2011-11-30 48 views
4

我有一個TextView,我想在頂部邊框上放一個不同的顏色(我們假設爲白色) 我嘗試類似這樣但不起作用(將白色邊框放在所有邊距上 - 左側,右,上,下)TextView Border不同顏色的頂部

<TextView 
android:layout_height="50dip" 
android:text="@string/total" 
android:background="@drawable/border_top_textview" 
android:textColor="#FFFFFF" 
android:id="@+id/totalCash" 
android:layout_width="match_parent" 
android:gravity="center_vertical" 
android:textSize="26dip" 
android:layout_weight="0.3" 
android:textStyle="bold"></TextView> 

和border_top_textview.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
<item> 
    <shape android:shape="rectangle"> 
     <stroke android:width="2dp" android:height="2dp" android:color="#FFFFFF" /> 

     <solid android:color="#000000" /> 
      <padding android:top="1dp" android:bottom="1dp" /> 
    </shape></item></layer-list> 
+0

看看這個帖子[有一個簡單的方法來添加邊框的頂部和一個Android查看的底部] [1] [1]:http://stackoverflow.com/questions/1598119/is-there-an-easy-添加邊界到頂部和底部的一個Android視圖 – mH16

+0

我看在你提到的帖子...仍然是同樣的問題,圍繞所有的TextView pt邊框,不僅在頂部 – tinti

+0

http://stackoverflow.com/questions/3263611/border-for-an-image-view-in-android – mH16

回答

3

簡單的方法是使用9補丁圖像作爲背景與期望的邊界。

試試這個,可能是它會幫助你,它會創建在頂部一條線,這會看起來像一個邊界:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item> 
    <shape android:shape="line"> 
     <stroke android:width="5dp" android:color="#FFFF00" /> 
     <solid android:color="#00000000" /> 

     <padding android:top="25dp" /> 

    </shape> 
</item> 
</layer-list> 
+0

我不想使用9補丁圖像...我認爲必須是僅使用xml或java類的解決方案。關於你的代碼是一個「黑客」,你把一行放在textview的中間,並把文本放在這行的下面。真的不是我想要的。 – tinti

+0

tinti,我認爲這會爲你做這項工作。 – mH16

2

在這裏,您可以把形狀標籤內此行,

<stroke android:width="4dp" android:color="#FFFFFF" /> 
<padding android:left="0dp" android:top="7dp" 
android:right="0dp" android:bottom="0dp" /> 

放頂邊距ATLEAST 5DP和讓別人0dp

+0

不工作...仍然所有邊框是白色 – tinti

2
public class MyTextView extends TextView { 

     public MyTextView(Context context, AttributeSet attrs, int defStyle) { 
      super(context, attrs, defStyle); 
     } 
     public MyTextView(Context context, AttributeSet attrs) { 
      super(context, attrs); 
     } 
     public MyTextView(Context context) { 
      super(context); 
     } 
     @Override 
     protected void onDraw(Canvas canvas) { 
      super.onDraw(canvas); 
      Rect rect = new Rect(); 
      Paint paint = new Paint(); 
      paint.setStyle(Paint.Style.STROKE); 
      paint.setColor(Color.WHITE); 
      paint.setStrokeWidth(3); 
      getLocalVisibleRect(rect); 
      canvas.drawRect(rect, paint);  
     } 
} 

XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
     <samples.test.MyTextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/hello" /> 
</LinearLayout> 

See here

試試這個棘手的方式來使用的圖像作爲邊界,並使用此代碼,它會正常工作。

enter code here 

    <TextView 
    android:id="@+id/text1" 
    android:layout_width="100dp" 
    android:layout_height="wrap_content" 
    android:drawableTop="@drawable/header1" 
    android:gravity="center" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:textStyle="bold"    
    android:textSize="20sp" 
    android:textColor="@drawable/selector_icon_text_color" 
    android:text="Hussain"> 
    </TextView> 
+0

也許我不清楚。我想只有頂部邊界是白色的。這個例子使所有邊框變成白色。 – tinti