作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的布局中有一个 TextInputEditText,我需要向其添加一个可绘制端。可绘制端出现,但我无法为其添加可绘制填充端。
下面是我试过的一段代码:
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.error), null);
editText.setCompoundDrawablePadding(getResources().getDimensionPixelSize(R.dimen.image_padding));
最佳答案
由于 getResources().getDrawable
已弃用,最好改用 ContextCompat.getDrawable()
。如果 drawablePadding
没有改变并且没有必要以编程方式处理它,请尝试在 xml
文件中设置它。
editText.setCompoundDrawablesWithIntrinsicBounds(
null,
null,
ContextCompat.getDrawable(context, R.drawable.error),
null
);
在布局 xml 中:
<android.support.design.widget.TextInputEditText
android:id="@+id/editText"
...
android:drawablePadding="@dimen/image_padding"
/>
.
如果您使用的是 android vector drawable 并希望向后兼容 API below 21,请添加以下代码段。
在应用级 build.gradle 中:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
在应用类中:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
}
关于Android setCompoundDrawablePadding 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53061982/
我想把图片放在按钮的文字上。我想将文本和图像之间的间距设置为零。 此外,还有一个尺寸为 200 dp x 200 dp 的按钮我希望将图像和文本布置在相同的垂直填充中。请帮助我。 Button
我的布局中有一个 TextInputEditText,我需要向其添加一个可绘制端。可绘制端出现,但我无法为其添加可绘制填充端。 下面是我试过的一段代码: editText.setCompoundD
我是一名优秀的程序员,十分优秀!