Aligning drawableLeft with text of button

后端 未结 14 1973
长情又很酷
长情又很酷 2021-01-30 06:41

Here is my layout:

\"enter

The issue I\'m facing is with the drawable checkmark. H

14条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 06:59

    I know it's a bit late, but if anyone looking for another answer, here is another way to add icon without the need to wrap button with a ViewGroup

    
    
    
        

    *need to set textAllCaps to false to make the spannable working


    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            val buttonLabelBuilder = SpannableStringBuilder(btnCamera.text)
            val iconDrawable = AppCompatResources.getDrawable(this, R.drawable.ic_camera)
            iconDrawable?.setBounds(0, 0, btnCamera.lineHeight, btnCamera.lineHeight)
            val imageSpan = ImageSpan(iconDrawable, ImageSpan.ALIGN_BOTTOM)
    
            buttonLabelBuilder.insert(0, "i ")
            buttonLabelBuilder.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    
            btnCamera.text = buttonLabelBuilder
        }
    }
    

提交回复
热议问题