Android:如何正确定位图像

时间:2014-05-14 08:05:00

标签: android image position

大家好我想知道如何使用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。

1 个答案:

答案 0 :(得分:2)

要将图片设置为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

相关问题