您可以尝试在设置drawable时,同时设置drawable的大小和textview的padding值,这样可以确保文字居中。例如:

TextView textView = findViewById(R.id.text_view);
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
drawable.setBounds(0, 0, 50, 50); //设置drawable大小
textView.setCompoundDrawables(drawable, null, null, null);
textView.setCompoundDrawablePadding(10); //设置textview的padding值

在上述代码中,我们设置了drawable的大小为50x50,并将textview的左侧drawable设置为该drawable。同时,我们设置了textview的padding值为10,这样可以确保文字居中。您可以根据实际情况调整drawable大小和padding值

我是安卓程序员textview设置drawable后文字居中失效

原文地址: https://www.cveoy.top/t/topic/c8Ox 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录