-3

我正在製作一個應用程序,讓我動態地製作表格。每個表格包括3個ImageViews和一個TextView,textview沒有任何問題,我只是簡單地使用textview.setGravity(Gravity.Center_Vertical),但ImageViews存在問題。如何居中(垂直和水平)?在TableLayout中動態調用中心ImageView

這裏的圖片wchich顯示我想達到的目標:enter image description here

這裏的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); 
    } 
} 
+0

我不太確定我明白你想達到的目標。請提供圖紙/截圖。 –

+0

看看http://stackoverflow.com/questions/2618331/how-to-set-gravity-or-margins-of-imageview-using-code –

+0

它不適合我 – user1275995

回答

0

嘗試這樣

ImageView imgView=new ImageView(this); 
imgView.setImageResource(R.drawable.yourimage); 
imgView.setScaleType(ImageView.ScaleType.FIT_XY); 
+0

不工作,它仍然對齊到Top of TableRow – user1275995

+0

分享一點點代碼... – NagarjunaReddy

+0

我將代碼添加到最初的問題 – user1275995

0

您是否嘗試過加入裏面的RelativeLayout

設置RelativeLayoutLayoutParams匹配要(對細胞的全尺寸)內畫的空間,然後加入ImageViewRelativeLayout時,與RelativeLayout.center_in_parent

1

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

相關問題