Android TextView可绘制,在可绘制和文本之间更改填充?
我正在setCompoundDrawablePadding(-75)中创建一个TextView,其下方带有可绘制对象。
我想将绘图对象带到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);
如何设置文本和可绘制到TextView的中间?
4个解决方案
112 votes
您需要结合drawablePadding和padding才能获得所需的结果。
ssantos answered 2020-02-19T22:37:30Z
10 votes
在XML中:
android:drawablePadding = paddingValue
要么
以编程方式:
TextView txt;
txt.setCompoundDrawablePadding(paddingValue)
android:drawablePadding是为可绘制图标提供填充的最简单方法,但是您不能给特定的一侧填充(例如可绘制图标的paddingRight或paddingLeft)。 这样可以在可绘制对象的任一侧填充。
Anubhav answered 2020-02-19T22:38:03Z
2 votes
android:id="@+id/otherapps"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/btn_background"
android:text="@string/other_apps"
android:layout_weight="1"
android:fontFamily="cursive"
android:layout_marginBottom="10dp"
android:drawableLeft="@drawable/ic_more"
android:paddingLeft="8dp" //for drawable padding to the left
android:textColor="@android:color/holo_red_light" />`enter code here`
Null Pointer Exception answered 2020-02-19T22:38:18Z
0 votes
您可以使用图层列表。
之前:
创建形状的xml-shape_rectangle.xml:
android:shape="rectangle">
android:width="5dp"
android:height="5dp" />
后:
在xml中,类似android:right的内容。您也可以添加top,left,bottom。 通过这种方式,您可以给出例如:在不调整textView的总高度的情况下向左右可填充区域进行填充。
deadfish answered 2020-02-19T22:38:56Z