Android GridLayout 中的 FrameLayouts 未显示

Android FrameLayouts in GridLayout not showing

我正在尝试制作类似下面的屏幕截图(来自学校的作业):

如作业中所述,我有一个 Activity PlayActivity:

public class PlayActivity extends ActionBarActivity {
    @Bind(R.id.activity_play_textView_score)
    TextView txtScore;

    @Bind(R.id.activity_play_board)
    Board board;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);

        ButterKnife.bind(this);
    }
}

此 activity 的 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/activity_play_textView_score"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/activity_play_textView_score_placeholder"/>

    <com.charlotteerpels.game2048.Board
        android:id="@+id/activity_play_board"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

我们还必须制作一个 class 卡片,它是 FrameLayout 的扩展:

public class Card extends FrameLayout {
    @Bind(R.id.card_frame_layout_textView)
    TextView txtNumber;

    private int number;

    public void setTxtNumber(TextView txtNumber) {
        this.txtNumber = txtNumber;
    }

    public TextView getTxtNumber() {
        return this.txtNumber;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getNumber() {
        return this.number;
    }

    public Card(Context context, AttributeSet attributeSet, int defaultStyle) {
        super(context, attributeSet, defaultStyle);
        inflateLayout();
        ButterKnife.bind(this);
    }

    public Card(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        inflateLayout();
        ButterKnife.bind(this);
    }

    public Card(Context context) {
        super(context);
        inflateLayout();
        ButterKnife.bind(this);
    }

    private void inflateLayout() {
        String infService = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li = (LayoutInflater)getContext().getSystemService(infService);
        li.inflate(R.layout.card_frame_layout, this, true);
    }
}

此 class 的 xml-布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/card_frame_layout_background"
    android:layout_margin="4dp">

    <TextView
        android:id="@+id/card_frame_layout_textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/card_frame_layout_textView_placeholder"
        android:textSize="40sp"
        android:layout_gravity="center"/>

</FrameLayout>

至少我们必须制作一个 class 板,它是 GridLayout 的扩展:

public class Board extends GridLayout {
    Card[][] cardBoard;

    public Board(Context context, AttributeSet attributeSet, int defaultStyle) {
        super(context, attributeSet, defaultStyle);
        initBoard(context);
    }

    public Board(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        initBoard(context);
    }

    public Board(Context context) {
        super(context);
        initBoard(context);
    }

    public void initBoard(Context context) {
        //Set the settings for the GridLayout (the grid is the board)
        String infService = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li = (LayoutInflater)getContext().getSystemService(infService);
        li.inflate(R.layout.board_grid_layout, this, true);

        //Initialize the cardBoard[][] and populate it
        cardBoard = new Card[4][4];

        for(int rij=0; rij<4; rij++) {
            for(int kolom=0; kolom<4; kolom++) {
                cardBoard[rij][kolom] = new Card(getContext());
            }
        }

        int cardMeasure = getCardMeasure(context);
        addCardBoardToGridLayout(cardMeasure);
    }

    private void addCardBoardToGridLayout(int cardMeasure) {
        for(int rij=0; rij<4; rij++) {
            for(int kolom=0; kolom<4; kolom++) {
                Card card = cardBoard[rij][kolom];
                addView(card, cardMeasure, cardMeasure);
            }
        }
    }

    private int getCardMeasure(Context context) {
        WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int screenWidth = size.x;
        return screenWidth/4;
    }
}

董事会的 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/board_grid_layout_background"
    android:columnCount="4"
    android:rowCount="4">
</GridLayout>

但我得到的只是:

我也在使用 Butterknife(网站:http://jakewharton.github.io/butterknife/) 用于绑定资源,但主要用于绑定来自 xml-layouts.

的视图和元素

有人可以帮我吗?

发现问题了!

所以在 Card 的 xml 布局中,我更改了 TextView 的 layout_width 和 layout_height:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/card_frame_layout_background"
    android:layout_margin="4dp">

    <TextView
        android:id="@+id/card_frame_layout_textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/card_frame_layout_textView_placeholder"
        android:textSize="40sp"
        android:layout_gravity="center"/>
</FrameLayout>

将"addCardBoardToGridLayout(int cardMeasure)"改为如下:

private void addCardBoardToGridLayout(int cardMeasure) {
    setRowCount(4);
    setColumnCount(4);

    removeAllViews();

    for(int rij=0; rij<4; rij++) {
        for(int kolom=0; kolom<4; kolom++) {
            Card card = cardBoard[rij][kolom];

            addView(card, cardMeasure, cardMeasure);
        }
    }
}

现在看起来像这样(完全符合我的要求!):