繁体   English   中英

Android:如何正确定位图像

[英]Android: How to position image correctly

大家好我想知道如何使用java而不是xml在liniearlayout中定位图像。 我正在创建这样的图像:

TextView Streets = new TextView(this);          
Streets.setText("Text");                        
Streets.setPadding(140, 25, 0, 25);
Streets.setCompoundDrawablesWithIntrinsicBounds (R.drawable.bus, 10, 10, 10);
Streets.setTag(i);

正如您所看到的,我正在设置图像的填充,但我想知道如何将图像设置为启动TextView。

要将图像设置为TextView的左侧(开始),您不需要ImageView

您可以使用

textView.setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom);

在您的情况下,您只需要在左侧设置drawable。

textView.setCompoundDrawablesWithIntrinsicBounds (R.drawable.bus, 0, 0, 0);

代码更新后:

您将函数10作为top,right和bottom的参数。 所以函数试图找到一个id等于10的drawable,它可能不存在。

请改用0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM