Android TextView drawable,在drawable和text之间更改填充?

Android TextView drawable,在drawable和text之间更改填充?,android,textview,drawable,padding,Android,Textview,Drawable,Padding,我正在创建一个TextView,在GridLayout中,下面有一个可绘制的视图 我想把绘图带到TextView的中间;我试过了 setCompoundDrawablePadding(-75)并且它只更改文本的位置 当前代码: TextView secondItem = new TextView(this); GridLayout.LayoutParams second = new GridLayout.LayoutParams(row2, col1); second.w

我正在创建一个
TextView
,在
GridLayout
中,下面有一个可绘制的视图

我想把绘图带到
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
的中间位置?

您需要将
drawablepading
padding
组合起来才能获得所需的结果。

您可以使用图层列表

之前:

为shape创建xml-shape_rectangle.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="5dp">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="@color/my_color" />
        <size
            android:width="5dp"
            android:height="5dp" />
    </shape>
</item>
</layer-list>

之后:


在xml中,有类似于android:right的东西。您还可以添加
顶部
左侧
底部
。通过这种方式,您可以给出例如:在不调整textView的总高度的情况下,将左侧和右侧填充为drawable。

在XML中:

android:drawablePadding = paddingValue
TextView txt;

txt.setCompoundDrawablePadding(paddingValue)

以编程方式:

android:drawablePadding = paddingValue
TextView txt;

txt.setCompoundDrawablePadding(paddingValue)
android:drawablePadding是为可绘制图标添加填充的最简单方法,但您不能像可绘制图标的paddingRightpaddingLeft那样添加特定的单面填充。这将为可绘制图形的任一侧提供填充。

`在此处输入代码`
  <Button
            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`

您将如何为不同的文本(例如不同的地区)组合它?