android drawablepadding 代码,Android TextView可绘制,在可绘制和文本之间更改填充?...

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

您可以使用图层列表。

之前:

7202eb23052ecc92e3361c87c97a96ed.png

创建形状的xml-shape_rectangle.xml:

android:shape="rectangle">

android:width="5dp"

android:height="5dp" />

后:

a5959c7fd9476055b2c330c8a2f4ee5f.png

在xml中,类似android:right的内容。您也可以添加top,left,bottom。 通过这种方式,您可以给出例如:在不调整textView的总高度的情况下向左右可填充区域进行填充。

deadfish answered 2020-02-19T22:38:56Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值