【问题标题】:Android FrameLayouts in GridLayout not showingGridLayout中的Android FrameLayouts未显示
【发布时间】:2015-12-09 11:01:22
【问题描述】:

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

正如作业中提到的,我有一个 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 布局:

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

我们还必须创建一个类 Card,它是 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 布局:

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

Board 的 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 布局的视图和元素。

谁能帮帮我?

【问题讨论】:

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


    【解决方案1】:

    找到问题所在!

    所以在 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>
    

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

    现在它看起来像这样(这正是我想要的!):

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 2017-05-07
      • 2016-05-08
      • 1970-01-01
      • 2017-12-06
      相关资源
      最近更新 更多