2016-08-05 200 views
10

如何添加文本周围边框为使用XML布局安卓:添加边框的TextView

image 1

我已经尝试添加边框布局,但它与文字重叠显示图像。

image 2

+0

可以使用形状绘制 – KrishnaJ

+0

给予的TextView的背景为白色,然后把它放在箱子 –

+0

上面可以请详细说明,我要补充的是矩形 – Jignesh

回答

15

你可以试试这个布局,它反映了按您的要求

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="15dp" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/border" 
      android:layout_marginTop="10dp" 
      android:orientation="vertical" 
      android:padding="15dp"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textStyle="bold" 
       android:text="Label 1: Value 1"/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textStyle="bold" 
       android:text="Label 2: Value 2"/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textStyle="bold" 
       android:text="Label 3: Value 3"/> 

     </LinearLayout> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:text=" Details " 
      android:layout_marginLeft="15dp" 
      android:background="#ffffff" 
      android:textSize="17sp" /> 

    </RelativeLayout> 

XML border.xml的

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

希望这有助于你以某种方式。

+0

非常感谢!! ..它的工作!! ... – Jignesh

+0

欢迎亲爱的... –

0
<?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="wrap_content" 
android:orientation="vertical"> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="16dip"> 

    <TextView 
     android:id="@+id/txt_notificaiton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dip" 
     android:background="@drawable/button_effect_white" 
     android:padding="16dip" 
     android:text="" 
     android:textColor="@drawable/text_color_white" 
     android:textSize="22dip" /> 

    <TextView 
     android:id="@+id/txt_createddateandtime" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right|bottom" 
     android:padding="16dp" 
     android:text="" 
     android:textSize="12dip" /> 

    <TextView 
     android:id="@+id/iv_read_unread" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left|top" 
     android:layout_marginRight="10dp" 
     android:background="@color/white" 
     android:text="Details" /> 

</FrameLayout> 
</LinearLayout> 
+0

您可以张贴布局的截屏内textviews,按要求 – Jignesh

+0

它不工作你可以根据需要将imageview更改为TextView。 – PriyankaChauhan

+0

@Jignesh请检查更新代码 – PriyankaChauhan

5

添加边框到Android的TextView,我们需要下绘制的文件夹中创建包含形状矩形文件的XML并将其设置为背景,以TextView的。

<stroke> tag is used to set the border width and color. 

border.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" > 
<stroke 
android:width="2dp" 
android:color="#000000" /> 
</shape> 

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:padding="10dp" xmlns:tools="http://schemas.android.com/tools" >        
<TextView 
android:id="@+id/textView2" 
android:layout_width="match_parent" 
android:layout_height="30dp" 
android:layout_centerHorizontal="true" 
android:layout_centerVertical="true" 
android:background="@drawable/border" 
android:gravity="center" 
android:text="Android Programming is fun!!" /> 
</RelativeLayout> 

如果你想要把一个边界的任何布局而不是TextView的,使布局背景

**android:background="@drawable/border"** 
+0

其在textview中添加边框,我需要在该边框矩形内放置几个textviews。 – Jignesh

+0

好的。然后,您可以使用线性或相对等任何布局进行设计。将边框添加到该布局。最后,您可以在该布局中添加文字浏览。希望你明白了。 –

+0

无法将textview放置在布局的边框上,请检查有问题的新图像 – Jignesh

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

<item android:state_pressed="true"> 
<shape > 
<solid android:color="@color/white"/> 
<corners 
android:radius="1dp"/> 
</shape> 
</item> 

<item android:state_focused="true" > 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 
<solid android:color="@color/white"/> 
<corners 
android:radius="1dp"/> 
</shape> 
</item> 

<item android:state_focused="false" > 


<shape xmlns:android="http://schemas.android.com/apk/res/android" > 

<solid android:color="#c38416"/> 
<stroke 
    android:width="3dp" 
    android:color="@color/bordercolor" /> 

<corners android:radius="3dp" /> 

<padding 
    android:bottom="6dp" 
    android:left="6dp" 
    android:right="6dp" 
    android:top="6dp" /> 

</shape> 
</item> 

<item android:state_pressed="false" > 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 
<solid android:color="#c38416"/> 
<corners 
android:radius="1dp" 
/> 
</shape> 

and 
    <TextView 
    android:background="@drawable/textbackground" 

    /> 
+0

无效,请检查新图片 – Jignesh

0

创建绘制文件,并在文件中添加以下代码

<?xml version="1.0" encoding="utf-8"?> 

    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 

     <corners android:radius="2dp" /> 
     <stroke 
      android:width="2px" 
      android:color="@color/colorGreyBrown" /> 
     <solid android:color="#ffffff" /> 
    </shape> 

那么这可绘制文件作为背景设置为您的TextView

<TextView 
       android:id="@+id/Textview_register_als" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
    android:background="@drawable/foodynet_rounded_corner_brown_color_border_white_bg" 
       android:gravity="center" 
       android:text="@string/string_text_register" 
       android:textColor="@color/colorGreyBrown" 
       android:textSize="@dimen/text" /> 
+0

无效,请检查新图片 – Jignesh