简体   繁体   中英

Android: How to position image correctly

Hi all I want to know how to position an image within a liniearlayout using java not xml. I am creating the image like this:

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);

As you can see I am setting the padding of the image but I want to know how to set the image to be toStartOf a TextView.

To set an image to the left (start) of a TextView you don't need an ImageView .

You can use

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

In your case you only need to set the drawable on the left.

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

After your code update:

You are giving the function 10 as an argument for top, right and bottom. So the function is trying to find a drawable which has an id equal to 10, which probably doesn't exist.

Use 0 instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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