[英]edittext align left
我正在为Android手机构建一个sudokusolver-app。
最初会向用户显示一个起始页,要求他/她键入板尺寸(4 * 4、6 * 6等)。 之后,开始新的活动,并绘制具有选定尺寸的电路板。 此面板由EditText
区域形状的正方形组成。 我正在努力使EditText
区域左侧的文本对齐。 在EditText
区域中键入的文本稍微向左对齐,但是我希望将文本放置在角落。 我该怎么做呢? 我只使用javacode而不是xml
import java.util.ArrayList;
import sudoku.androis.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.text.Layout;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class SudokuUI extends Activity{
int dim;
EditText[][] board;
ScrollView sv;
RelativeLayout ll;
int params;
int boxHeight;
int boxWidth;
int textSize;
Button neste;
Button første;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
dim = getIntent().getExtras().getInt("key");
TextView textview = new TextView(this);
textview.setText(dim + "*" + dim + " brett");
if(dim == 4) {
params = 100;
boxHeight = 2;
boxWidth = 2;
textSize = 20;
}
else if(dim == 6) {
params = 70;
boxHeight = 2;
boxWidth = 3;
textSize = 20;
}
else if (dim == 9) {
params = 50;
boxHeight = 3;
boxWidth = 3;
textSize = 12;
}
else if (dim == 12) {
params = 40;
boxHeight = 3;
boxWidth = 4;
textSize = 5;
}
else if(dim == 16){
params = 30;
boxHeight = 4;
boxWidth = 4;
textSize = 2;
}
sv = new ScrollView(this);
ll = new RelativeLayout(this);
ll.addView(textview);
ll.setBackgroundDrawable(getResources().getDrawable(R.drawable.background1));
//ll.addView(sv);
board = new EditText[dim][dim];
for(int i = 0; i < dim; i++) {
for(int j = 0; j < dim; j++) {
board[i][j] = createEditText();
//board[i][j].setTextSize(textSize);
board[i][j].setBackgroundDrawable(getResources().getDrawable(R.drawable.square));
board[i][j].setTextColor(Color.parseColor("#ff0000"));
LinearLayout.LayoutParams marginParams = new LinearLayout.LayoutParams(board[i][j].getLayoutParams());
int paramOne = 0;
int paramTwo = 0;
for(int b = 1; b <= boxHeight; b++) {
for(int c = 1; c <= boxWidth; c++) {
if(i == boxWidth*b){
paramOne = 6;
} else if(j == boxHeight*c) {
paramTwo = 6;
}
}
}
marginParams.setMargins((i)*params + paramOne, (j+1)*params + paramTwo,0,0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
board[i][j].setLayoutParams(layoutParams);
ll.addView(board[i][j]);
}
}
neste = createNesteButton();
neste.setText("Neste");
LinearLayout.LayoutParams buttonPar = new LinearLayout.LayoutParams(neste.getLayoutParams());
buttonPar.setMargins(220,550,0,0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(buttonPar);
neste.setLayoutParams(layoutParams);
ll.addView(neste);
første = createNesteButton();
første.setText("Start");
LinearLayout.LayoutParams buttonPar2 = new LinearLayout.LayoutParams(første.getLayoutParams());
buttonPar2.setMargins(0,550,0,0);
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(buttonPar2);
første.setLayoutParams(layoutParams2);
ll.addView(første);
første.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int[][] numbers = new int[dim][dim];
for(int i = 0; i < dim; i++) {
for(int j = 0; j < dim; j++) {
try {
numbers[i][j] = Integer.parseInt(board[i][j].getText().toString());
validNumber(Integer.parseInt(board[i][j].getText().toString()));
} catch (NumberFormatException ne) {
numbers[i][j] = 0;
} catch (TooHighNumberException te) {
numbers[i][j] = 0;
}
}
}
}
});
ll.addView(sv);
setContentView(ll);
}
public EditText createEditText(){
final LayoutParams lparams;
lparams = new LayoutParams(params,params);
final EditText edittext = new EditText(this);
edittext.setGravity(Gravity.FILL_HORIZONTAL);
edittext.setLayoutParams(lparams);
return edittext;
}
public Button createNesteButton() {
final LayoutParams lparams;
lparams = new LayoutParams(200, 80);
final Button b = new Button(this);
b.setLayoutParams(lparams);
return b;
}
public void validNumber(int nr) throws TooHighNumberException {
if(nr > dim || nr < 0) {
throw new TooHighNumberException();
}
}
}
class TooHighNumberException extends Exception{ }
设置EditText属性, 在Eclipse中,使用“属性”选项卡,将Gravity设置为EditText的左侧。
android:gravity = "left";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.