在linearlayout中的特定x位置添加自定义imageview

时间:2015-01-12 10:09:12

标签: android android-linearlayout android-imageview

我正在从edittext的建议列表中添加关于项目选择的imageview。下面是我的代码:

edt1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

                String path = adapter1.getSelectedItem(position).getPath();

                Bitmap btmp = getBitmapFromAsset("imgs", path);

                int pos = edt1.getSelectionStart();

                String myText = edt1.getText().toString();

                String subStringed = myText.substring(0, pos);
                String[] parts = subStringed.split(" ");
                String lastWord = parts[parts.length - 1];

                Log.e("WORD--->", "LAST---> "+lastWord);

                Log.e("WORD--->", "POS---> "+pos);

                int newpos = pos - lastWord.length()-1;

                Log.e("WORD--->", "NEW POS---> "+newpos);


                Layout layout = edt1.getLayout();
                int line = layout.getLineForOffset(newpos);
                int baseline = layout.getLineBaseline(line);
                int ascent = layout.getLineAscent(line);
                float x = layout.getPrimaryHorizontal(newpos);
                float y = baseline + ascent;


                if(parts.length>imgList.size()){
                    ImageView iv = new ImageView(getActivity());
                    iv.setImageBitmap(btmp);
                    iv.setX(x);
                    llImgContainer.addView(iv);
                    imgList.add(iv);
                }else{
                    imgList.get(parts.length-1).setImageBitmap(btmp);
                    imgList.get(parts.length-1).setX(x);
                }

            }
        });

我将图像添加到linearlayout中,这是在最后一个单词的起始位置的edittext上。但它没有完美地设置在x位置。你可以在下图中看到:

enter image description here

红线是单词的起始位置,并在该位置上设置图像。但你可以看到上面的edittext,图像设置不完美。帮我解决这个问题。

0 个答案:

没有答案