简体   繁体   中英

How can I add TextView horizontally?

I am new in programming android, and I'm developing a BattleShip game . I created a 10x10 board , and now I'm trying to add the random ships on the board. I found the positions through the getPositionOfTag , and through it completed the ship with another textview with vertical orientation . I wonder how I put more textviews the ship in order to supplement it with its true size ( 5 squares). And also like to know how to put it ramdom horizontally.

Java Code:

 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 Class:

 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); 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM