GridLayout中的Android FrameLayouts没有显示

时间:2015-12-09 11:01:23

标签: android xml grid-layout android-framelayout custom-view

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

Desired board layout

正如作业中所提到的,我有一个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);
    }
}

此活动的xml-layout:

<?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>

我们还必须制作一个类卡,它是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);
    }
}

此类的xml-layout:

<?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>

至少我们必须制作一个类Board,它是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>

但我得到的只是:

Current board

我也在使用Butterknife(网站:http://jakewharton.github.io/butterknife/) 用于绑定资源,但主要用于绑定xml-layouts中的视图和元素。

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

发现了什么问题!

所以在Card的xml-layout中,我从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>

&#34; addCardBoardToGridLayout(int cardMeasure)&#34;改为:

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

现在它看起来像这样(它就像我想要的那样!): Resulting board