在TableLayout中动态调用中心ImageView

我正在制作一个应用程序,让我动态地制作表格。每个表格包括3个ImageViews和一个TextView,textview没有任何问题,我只是简单地使用textview.setGravity(Gravity.Center_Vertical),但ImageViews存在问题。如何居中(垂直和水平)?在TableLayout中动态调用中心ImageView

这里的图片wchich显示我想达到的目标:

这里的wchich ADSS一切TableLayout类:

public class Obiekt { 

int moje;

TableRow tr = new TableRow(MainActivity.this);

ImageView iv = new ImageView(MainActivity.this);

ImageView sett = new ImageView(MainActivity.this);

ImageView rin = new ImageView(MainActivity.this);

public Obiekt(int obraz, int obraz2, int obraz3, int dzwiek, String tekst) {

moje = sound.load(MainActivity.this, dzwiek, 1);

iv.setImageResource(obraz);

//iv.setLayoutParams(rowparams);

tr.addView(iv);

// TEXTVIEW settings

TextView tv = new TextView(MainActivity.this);

tv.setText(tekst);

tv.setTypeface(tf);

tv.setTextSize(rozmiar);

tv.setGravity(Gravity.CENTER_VERTICAL);

tv.setGravity(Gravity.CENTER_HORIZONTAL);

tv.setTextColor(Color.parseColor("#FFFFFF"));

tr.addView(tv);

// ImageView RIGHT

sett.setImageResource(obraz2);

tr.addView(sett);

// ImageView RING

rin.setImageResource(obraz3);

tr.addView(rin);

//tr.setLayoutParams(rowparams1);

tl.addView(tr);

View wiew = new View(MainActivity.this);

wiew.setBackgroundResource(R.drawable.divide);

tl.addView(wiew);

tv.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

play();

}

});

}

public void hide() {

tr.setVisibility(10);

}

public void size() {

iv.getLayoutParams().height=rozmiar;

iv.getLayoutParams().width=rozmiar;

sett.getLayoutParams().width=rozmiar;

sett.getLayoutParams().height=rozmiar;

rin.getLayoutParams().height=rozmiar;

rin.getLayoutParams().width=rozmiar;

}

public void play() {

sound.play(moje, 1, 1, 0, 0, 1);

}

}

回答:

尝试这样

ImageView imgView=new ImageView(this); 

imgView.setImageResource(R.drawable.yourimage);

imgView.setScaleType(ImageView.ScaleType.FIT_XY);

回答:

您是否尝试过加入里面的RelativeLayout

设置RelativeLayoutLayoutParams匹配要(对细胞的全尺寸)内画的空间,然后加入ImageViewRelativeLayout时,与RelativeLayout.center_in_parent

回答:

设置你的imageviews添加它“ SCALE TYPE“to FitXY like imageview is imageView than imageView.setScaleType(ScaleType.FIT_XY);

以上是 在TableLayout中动态调用中心ImageView 的全部内容, 来源链接: utcz.com/qa/266155.html

回到顶部