2011-11-30 77 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