我在 GridLayout
中创建一个 TextView
,下面有一个可绘制对象。
我想把drawable放到TextView
的中间;我试过了
setCompoundDrawablePadding(-75)
,它只改变文本的位置。
当前代码:
TextView secondItem = new TextView(this);
GridLayout.LayoutParams second = new GridLayout.LayoutParams(row2, col1);
second.width = halfOfScreenWidth;
second.height = (int) (quarterScreenWidth * 1.5);
secondItem.setLayoutParams(second);
secondItem.setBackgroundResource(R.color.MenuSecond);
secondItem.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, R.drawable.ic_action_new);
secondItem.setText("Text");
secondItem.setCompoundDrawablePadding(-180);
secondItem.setGravity(Gravity.CENTER);
secondItem.setTextAppearance(this, android.R.style.TextAppearance_Large_Inverse);
gridLayout.addView(secondItem, second);
如何将文本和drawable设置在TextView
的中间?
最佳答案
您需要结合 drawablePadding
和 padding
以获得所需的结果。
关于Android TextView 可绘制,更改可绘制和文本之间的填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19362762/