2017-08-08 44 views
0

我堅持在Textview的末尾添加一個標籤。或者可能會在Textview的最後一段文字中出現邊框。如何邊框一塊TextView

諸如(熱,趨勢,..等等)的標籤必須位於標題的末尾。與冠軍同一條線上,如果有空間的背後,或新的生產線,如果不

請看下面的截圖

更新(上傳XML代碼)

bg_border_label_test.xml

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

item_post。 XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/rlContainer" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginLeft="16sp" 
android:layout_marginRight="16sp"> 

<ImageView 
    android:id="@+id/imvPostImage" 
    android:layout_width="120dp" 
    android:layout_height="80dp" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="16sp" 
    android:background="#df7217" 
    android:scaleType="centerCrop" /> 

<TextView 
    android:id="@+id/tvPostTitle" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/imvPostImage" 
    android:layout_marginLeft="16sp" 
    android:layout_toEndOf="@+id/imvPostImage" 
    android:layout_toRightOf="@+id/imvPostImage" 
    android:ellipsize="end" 
    android:ems="10" 
    android:lineSpacingExtra="-3dp" 
    android:maxLines="3" 
    android:textColor="#000" 
    android:textSize="16sp" 
    tools:text="Alan Walker vs Coldplay - Hymn For The Weekend" /> 


<TextView 
    android:id="@+id/tvPostLabel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/tvPostTitle" 
    android:layout_alignParentRight="true" 
    android:background="@drawable/bg_border_label_test" 
    android:maxLines="1" 
    android:paddingLeft="8dp" 
    android:paddingRight="8dp" 
    android:textColor="#a30014" 
    android:textSize="12sp" 
    android:textStyle="bold" 
    tools:text="HOT" /> 

<mgift.views.component.TextViewPlus 
    android:id="@+id/tvPublishDate" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/imvPostImage" 
    android:layout_alignLeft="@+id/tvPostTitle" 
    android:maxLines="1" 
    android:textColor="@color/text_manatee" 
    android:textSize="@dimen/v4_small_size_text" 
    app:font="@string/open_sans_regular" 
    tools:text="3 hours ago" /> 

當前的結果:

enter image description here

期待的結果(標籤HOT稱號後總是)

enter image description here

回答

1

您可以創建一個可繪製,並將其設置爲背景爲您textView,看示例代碼如下 -

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

<stroke android:width="6px" android:color="#ff0000"/> 

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

    <corners android:radius="30dp" /> 
</shape> 

此外,如果要在文本的末尾連接特定標籤,則可以創建該標籤的圖像,然後使用SpannableString在文本之後連接圖像。

例如,你可以參考下面的代碼 -

SpannableStringBuilder builder = new SpannableStringBuilder(); 
builder.append("Alan Walker vs Coldplay - Hymn "); 
String hot = "Hot"; 
Drawable d = getResources().getDrawable(R.drawable.ic_hot_label_24dp); 
d.setBounds(0, 16, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); 
SpannableString hotSpannable= new SpannableString(hot); 
hotSpannable.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 
builder.append(hotSpannable); 
tvPostTitle.setText(builder, TextView.BufferType.SPANNABLE); 

讓我知道如果你需要更多的幫助。

+0

謝謝你,但背景繪製不是我的問題。標籤必須位於標題的末尾。如果後面有空格,則與標題相同,否則換行。 –

+0

您可以使用RelativeLayout作爲您的父級佈局,並添加一個元素,如android:layout_toRightOf =「@ + id/text_view_before_label」 – hsm59

+0

如果TextView有2行,第一行填滿,第二行持續一半。標籤位置在哪裏? –

0

Drawable中添加以下代碼。例如:edit_background.xml

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

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

    <solid android:color="#ffffff" /> 

    <stroke 
    android:width="3px" 
    android:color="#918e8e"/> 

</shape> 

並調用佈局這個名字(你的TextView下):

android:background="@drawable/edit_background" 
相關問題