如何水平添加TextView?

问题描述:

我是编程android新手,我正在开发一个BattleShip游戏。我创建了一个10x10板,现在我正在试图在板上添加随机船。我通过getPositionOfTag找到了位置,并通过它完成了另一个垂直方向的文本视图。我想知道我是如何把它的文本视图放在船上以便用它的真实尺寸(5格)来补充它。而且还想知道如何水平放置ramdom。如何水平添加TextView?

Java代码:

for (int i = 0; i < tamanho_tabuleiro; i++) { 
 

 
      tableRow = new TableRow(this); 
 
      tableRow.setLayoutParams(rowParams); 
 

 
      for (int j = 0; j < tamanho_tabuleiro; j++) { 
 

 

 
       textView = new TextView(this); 
 
       textView.setBackgroundDrawable(gd); 
 

 
       textView.setLayoutParams(rowParams); 
 
       //textView.setText(" "); 
 
       textView.getLayoutParams().height = lado_quadrado; 
 
       textView.getLayoutParams().width = lado_quadrado; 
 
       textView.setTag(String.valueOf(i + "." + j)); 
 

 
       textView.setOnClickListener(new View.OnClickListener() { 
 

 
        @Override 
 
        public void onClick(View v) { 
 

 
         v.setBackgroundDrawable(gradientDrawable); 
 
        } 
 
       }); 
 

 
       tableRow.addView(textView); 
 
      } 
 

 
      tableLayout.addView(tableRow, tableParams); 
 
     } 
 

 
     for (int k = 0; k < qtd_portaAvioes; k++) { 
 

 
      Random random = new Random(); 
 

 
      int indexI = random.nextInt(tamanho_tabuleiro); 
 
      int indexJ = random.nextInt(tamanho_tabuleiro); 
 

 
      TableRow tableRow = (TableRow) tableLayout.getChildAt(indexI); 
 

 
      View view = tableRow.findViewWithTag(indexI + "." + indexJ); 
 

 
      if (view != null && !verificaPopulado(view)) { 
 
       int positionComeco = getPositionOfTag((String) view.getTag()); 
 
        addNavio(tableRow, new PortaAvioes(this), positionComeco); 
 

 
       int orientation = random.nextInt(2); 
 

 
       for(int x = 0; x < tam_portaAvioes; x++) { 
 

 
        TableRow tableRow1 = (TableRow) tableLayout.getChildAt(indexI + 1); 
 

 
        if (orientation == 0) { 
 
        //verificar sea a tablerow existe, se nao esta acima do limite 
 
         if (tableRow1 != null) { 
 
          View view2 = tableRow1.findViewWithTag(indexI + 1 + "." + positionComeco); 
 

 
          if (view2 != null && !verificaPopulado(view2)) { 
 
           addNavio(tableRow1, new PortaAvioes(this), positionComeco); 
 
          } 
 
         } else { 
 
          View view2 = tableRow1.findViewWithTag(indexI + 1 + "." + positionComeco); 
 
         } 
 
        }else if (orientation == 1) { 
 
         //add ship horizontal 
 
        } 
 
       } 
 
      } 
 
     } 
 

 

 
public void addNavio(TableRow tableRow, TextView navio, int position) { 
 
     tableRow.removeViewAt(position); 
 
     tableRow.addView(navio, position); 
 
    } 
 

 
    public boolean verificaPopulado(View view) { 
 
     if (view instanceof Encouracado || view instanceof Destroyer || view instanceof PortaAvioes || view instanceof Cruiser || view instanceof Submarino) 
 
      return true; 
 

 
     return false; 
 
    } 
 

 
    public int getPositionOfTag(String tag) { 
 
     return Integer.parseInt(tag.substring(tag.indexOf(".") + 1)); 
 
    }

PortaAvioes类:

public class PortaAvioes extends TextView { 
 
    
 

 
    public PortaAvioes(Context context) { 
 
     super(context); 
 

 
     GradientDrawable gd = new GradientDrawable(); 
 
     gd.setStroke(1, 0xFF000000); 
 
     gd.setColor(Color.YELLOW); 
 
     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
 
      setText("PA"); 
 
      setBackground(gd); 
 
     } else 
 
      setBackgroundDrawable(gd);

+0

use textview width = 10dp height = fill parent – raj

RotateAnimation rotateAnmi = (RotateAnimation) AnimationUtils.loadAnimation(this,R.drawable.rorate); 
rotateTv.setAnimation(rotateAnmi);  
    rotateTv.setGravity(Gravity.AXIS_X_SHIFT); 
    rotateTv.setText();