2015-12-14 34 views
3

這不是一個重複的問題,我發現了一些問題,但他們沒有滿意的答案,答案不適用於多行textviews。通過多行TextView的罷工

我已經檢查這個下面的鏈接,但他們沒有足夠的有用: Is there an easy way to strike through text in an app widget?

我想整個的TextView(刪除線TextView)中心創建一個線,但我想這樣做在XML佈局,而不是在Java代碼中。

我已經通過使用Java代碼實現了這個結果,這樣的:

myTextView.setPaintFlags(myTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 

我想同樣的結果,但在下面的XML代碼:

<TextView 
    android:id="@+id/myTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    /> 

enter image description here

再次感謝。

+1

這不是一個確切的重複。答案是一樣的,但問題是關於一個小部件,它具有不同的限制。 – yedidyak

+0

@yedidyak,謝謝你得到它 – Androider

回答

0

在xml中這樣做的方法是創建一個可拖動的罷工並將其設置爲TextView的背景。一個可繪製的例子是:

<item android:state_pressed="false"> 
    <shape android:shape="line"> 
     <stroke android:width="2dp" android:color="#ffffff" /> 
    </shape> 
</item> 
1

最簡單的方法是使用圖像。

創建帶有水平線和透明的背景圖像,並將其設置爲TextView

android:background="@drawable/strike_through" 

或者

後臺創建一個XML文件,該文件將畫一條線,並設置爲您TextView的背景

<shape android:shape="line"> 
    <stroke android:width="2dp" android:color="#ffffff" /> 
</shape> 
+0

我認爲你的xml代碼沒有完成,對於圖像我們應該需要9補丁圖像, – Androider

+0

它會工作在多行textview,使用圖像 – Androider

+0

好吧。我要更新圖片 – Androider

1
<TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/white" 
     android:text="XYZ" 
     android:background="@drawable/line"/> 

line.xml

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

    <stroke android:width="1dp" 
     android:color="@android:color/white"/> 

</shape> 

試試這個!

編輯

上面的代碼將只能用於單行文本。 對於下面的代碼多行文字使用:

<string name="strike_text"> 
     <strike>sample TextView \nSecond line of TextView</strike> 
</string> 

並以此

android:text="@string/strike_text" 
+0

確切的代碼,但不能用新行(多行)爲文本工作 – Androider

+0

@Androider看到我的編輯 –

+0

但字符串是動態的。 – Androider