change font for editText hint

后端 未结 10 2471
野趣味
野趣味 2020-12-03 13:17

Is it possible to change the font for the hint displayed in the EditText field? I want to set the font in the xml itself.

10条回答
  •  清歌不尽
    2020-12-03 14:20

    With Android 8.0 (API level 26) comes a new feature that gives a possibility to change font in xml for the hint displayed in the EditText. Guide based on Fonts in XML.

    Steps:

    1) Create a new resource directory: right-click the res folder and go to New -> Android resource directory.

    2) Set name of resource directory as font.

    3) In the Resource type list, select font, and then click OK.

    4) Add your custom font(e.g. my_font.ttf) in the font folder.

    5) In the layout XML, set the fontFamily attribute to the font file:

    
    

    ... or if you want to do it programmatically:

    val typeface = resources.getFont(R.font.my_font)
    edit_text.typeface = typeface
    

    To use the Fonts in XML feature on devices running Android 4.1 (API level 16) and higher, use the Support Library 26:

    Note: When you declare font families in XML layout through the support library, use the app namespace to ensure your fonts load.

    1) In font directory create custom_font.xml

    
    
        
    
    

    2) Set the fontFamily attribute:

     
    

    ... or if you want to do it programmatically:

    val typeface = ResourcesCompat.getFont(context, R.font.custom_font)
    edit_text.typeface = typeface
    

提交回复
热议问题