How to Underline Text of Button in Android?

前端 未结 5 922
鱼传尺愫
鱼传尺愫 2020-12-23 20:58

I have a button which is transparent and has an icon and text.

I want to underline the text of the button but i have not been able to do this.

Below is my

5条回答
  •  旧时难觅i
    2020-12-23 21:14

    Code only

    Java:

    Button button = (Button) findViewById(R.id.park);
    button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    

    Kotlin:

    val button = findViewById

    Resource string with static text (xml only)

    If you have a static text in your resources you could also use the following approach in your strings.xml:

    I\'m underlined
    

    Resource string with dynamic text (xml + code)

    If you're using dynamic text but don't like the first approach (which isn't the best imho either), you could also use following:

    strings.xml

    %s
    

    Java:

    button.setText(getString(R.string.underlined_dynamic_text, "I'm underlined");
    

    Kotlin:

    button.text = getString(R.string.underlined_dynamic_text, "I'm underlined")
    

提交回复
热议问题