2014-06-24 90 views
0

在我的应用程序中,我用一个文本和一个小图标(如带有自定义适配器的列表视图)填充一个线性布局。 我需要文本和图片具有相同的大小。有没有办法做到这一点?在textview上对齐图像和文本

下面的代码:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
       LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); 
     LinearLayout LayoutPadre = new LinearLayout(this); 
TextView Tag = new TextView(this); 
     ImageView img = new ImageView(this); 

     Tag.setText(data); 
    if(stato.equalsIgnoreCase("presente")){ 
     img.setImageResource(R.drawable.ic_presente); 
    }else{ 
     img.setImageResource(R.drawable.ic_assente); 
    } 

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(75, LayoutParams.WRAP_CONTENT); //Here I need the img to have same size as the text, but Tag.getHeight() returns 0 
     layoutParams.setMargins(50, 0, 0, 0); 

     img.setLayoutParams(layoutParams); 

     LayoutPadre.setLayoutParams(params); 

     LayoutPadre.addView(Tag); 
     LayoutPadre.addView(img); 

的问题是图像不水平居中比如img。 看看屏幕Sreen capture

谢谢大家。

回答

1

保持文本和图像的layout_height作为Match_Parent而不是Wrap_Content ......到那个

+0

感谢的!这行得通!!我maa很多尝试,但不是thant ..再次感谢! – Lele

+0

没问题..不客气 – Siva