我试图重新制作游戏2048.
当我点击按钮" New Game"时,我开始使用PlayActivity的新意图。该按钮启动活动,但是当视图显示时,网格布局不可见......
视图是textview,然后是GridLayout(这是自定义的,因为我必须使用自定义切片填充它)...
代码:
PlayActivity.java
public class PlayActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
}
}
activity_play.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:id="@+id/txtScore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/score"
android:background="@color/dark_grey"/>
<com.charlotteerpels.oefening1.Board
android:id="@+id/board"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.charlotteerpels.oefening1.Board>
</LinearLayout>
Board.java
public class Board extends GridLayout {
Card[][] cardBoard;
private int cardHeight;
private int cardWidth;
public void initBoard(Context context) {
this.setColumnCount(4);
this.setRowCount(4);
this.cardBoard = new Card[4][4];
calculateCardSpecs(context);
populateBoard();
}
private void calculateCardSpecs(Context context) {
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
this.cardWidth = width/4;
this.cardHeight = this.cardWidth;
}
private void populateBoard() {
int tilesWithNumber = 0;
Random random = new Random();
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
Card card = new Card(getContext());
if(random.nextInt(2) == 0) {
if(tilesWithNumber<2) {
card.setTextVisible();
tilesWithNumber++;
}
}
cardBoard[i][j] = card;
}
}
addCardsToView();
}
private void addCardsToView() {
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
Card card = cardBoard[i][j];
addView(card, cardWidth, cardHeight);
}
}
}
public Board(Context context) {
super(context);
initBoard(context);
}
public Board(Context context, AttributeSet attrs) {
super(context, attrs);
initBoard(context);
}
public Board(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initBoard(context);
}
}
Card.java
public class Card extends FrameLayout {
Random randomGenerator;
private int number;
Resources res = getResources();
private TextView lblNumber;
public void setNumber(int number) {
this.number = number;
}
public int getNumber() {
return this.number;
}
private void initCard(Context context) {
lblNumber = new TextView(context);
lblNumber.setBackgroundColor(res.getColor(R.color.light_brown));
randomGenerator = new Random();
int r = randomGenerator.nextInt(2);
if(r == 0)
this.number = 2;
else
this.number = 4;
}
public void setTextVisible() {
lblNumber.setText(String.valueOf(number));
}
public Card(Context context) {
super(context);
initCard(context);
}
public Card(Context context, AttributeSet attrs) {
super(context, attrs);
initCard(context);
}
public Card(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initCard(context);
}
}
有没有人知道我做错了什么? 提前谢谢!
答案 0 :(得分:0)
所以在initCard方法的类Card中,我在if / else后将textView lblNumber添加到了Card布局中:
private void initCard(Context context) {
lblNumber = new TextView(context);
lblNumber.setBackgroundColor(res.getColor(R.color.light_brown));
randomGenerator = new Random();
int r = randomGenerator.nextInt(2);
if(r == 0)
this.number = 2;
else
this.number = 4;
addView(lblNumber);
}