creating a strikethrough text?

前端 未结 11 887
借酒劲吻你
借酒劲吻你 2020-11-30 17:36

Can I create a strikethrough text in Android, I mean adding a special value in the TextView tag that can make this possible?



        
相关标签:
11条回答
  • 2020-11-30 18:29

    If you are using Kotlin:

    your_text_view.apply {
        paintFlags = paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
        text = "Striked thru text"
    }
    
    0 讨论(0)
  • 2020-11-30 18:32

    In your observable view model

    fun getStrikeContent(): String {
        return "Hello"
    }
    
    
     companion object {
        @BindingAdapter("strike")
        @JvmStatic
        fun loadOldPrice(view: TextView, value: String) {
            view.text = value
            view.paintFlags = STRIKE_THRU_TEXT_FLAG
        }
    }
    

    then in your xml

         <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             app:strike="@{itemViewModel.strikeContent}" />
    
    0 讨论(0)
  • 2020-11-30 18:34

    In Kotlin you can create extension property:

    inline var TextView.strike: Boolean
        set(visible) {
            paintFlags = if (visible) paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
            else paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
        }
        get() = paintFlags and Paint.STRIKE_THRU_TEXT_FLAG == Paint.STRIKE_THRU_TEXT_FLAG
    

    And use:

    textView.strike = true
    
    0 讨论(0)
  • 2020-11-30 18:35

    It is really easy if you are using strings:

    <string name="line"> Not crossed <strike> crossed </strike> </string>
    

    And then just:

    <TextView 
            ...
             android:text="@string/line"
     />
    
    0 讨论(0)
  • 2020-11-30 18:36

    Just use this and you are done . For Activity :

    TextView t= (TextView).findViewById(R.id.thousand));
    t.setPaintFlags(t.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    

    For Xml :

    <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
    
                <TextView
                    android:id="@+id/text_view_original_cash_amount"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:textColor="@android:color/darker_gray"
                    android:text="Rs. 1,999"/>
    
                <View
                    android:layout_width="wrap_content"
                    android:layout_height="1dp"
                    android:background="@android:color/darker_gray"
                    android:layout_centerVertical="true"
                    android:layout_alignStart="@id/text_view_original_cash_amount"
                    android:layout_alignEnd="@id/text_view_original_cash_amount"
                    android:layout_alignLeft="@id/text_view_original_cash_amount"
                    android:layout_alignRight="@id/text_view_original_cash_amount" /> 
    </RelativeLayout>
    
    0 讨论(0)
提交回复
热议问题