视图从屏幕上移开(Android)

时间:2017-03-24 13:45:56

标签: android layout screen overflow

我的TicTacToe有问题。该板必须是4x4正方形,元素也必须是正方形。已应用GridLayout。问题是在小屏幕上(也在横向模式下),电路板会从屏幕上移开。

以下是它的样子:

landscape

small screen

这是代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout                 xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kasia.tictactoe.BoardActivity"
android:id="@+id/parent">



<android.support.constraint.ConstraintLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="@id/parent"
    app:layout_constraintTop_toTopOf="@id/parent"
    app:layout_constraintStart_toStartOf="@id/parent"
    app:layout_constraintEnd_toEndOf="@id/parent"
    android:id="@+id/boardLayout"

    >

<com.example.kasia.tictactoe.MyGridLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:rowCount="4"
    android:columnCount="4"
    android:id="@+id/gridLayout"
    android:layout_centerInParent="true"
    app:layout_constraintBottom_toBottomOf="@id/boardLayout"
    app:layout_constraintTop_toTopOf="@id/boardLayout"
    app:layout_constraintRight_toRightOf="@id/boardLayout"
    app:layout_constraintLeft_toLeftOf="@id/boardLayout"



    >


    <com.example.kasia.tictactoe.GridViewButton    xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:scaleType="centerCrop"
        android:layout_gravity="fill_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </com.example.kasia.tictactoe.GridViewButton>

    <com.example.kasia.tictactoe.GridViewButton xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:layout_gravity="fill_horizontal"
        android:scaleType="centerCrop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
    </com.example.kasia.tictactoe.GridViewButton>

    <com.example.kasia.tictactoe.GridViewButton xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:scaleType="centerCrop"
        android:layout_gravity="fill_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
    </com.example.kasia.tictactoe.GridViewButton>



    <com.example.kasia.tictactoe.GridViewButton xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:scaleType="centerCrop"
        android:layout_gravity="fill_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
    </com.example.kasia.tictactoe.GridViewButton>


    <com.example.kasia.tictactoe.GridViewButton xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:scaleType="centerCrop"
        android:layout_gravity="fill_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"


        >
    </com.example.kasia.tictactoe.GridViewButton>
    <com.example.kasia.tictactoe.GridViewButton xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:scaleType="centerCrop"
        android:layout_gravity="fill_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        >
    </com.example.kasia.tictactoe.GridViewButton>



    <com.example.kasia.tictactoe.GridViewButton xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:scaleType="centerCrop"
        android:layout_gravity="fill_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        >
    </com.example.kasia.tictactoe.GridViewButton>
    <com.example.kasia.tictactoe.GridViewButton xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:scaleType="centerCrop"
        android:layout_gravity="fill_horizontal"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        >
    </com.example.kasia.tictactoe.GridViewButton>
    <com.example.kasia.tictactoe.GridViewButton xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:scaleType="centerCrop"
        android:layout_gravity="fill_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        >
    </com.example.kasia.tictactoe.GridViewButton>

    <com.example.kasia.tictactoe.GridViewButton xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:scaleType="centerCrop"
        android:layout_gravity="fill_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        >
    </com.example.kasia.tictactoe.GridViewButton>
    <com.example.kasia.tictactoe.GridViewButton xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:layout_gravity="fill_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        >
    </com.example.kasia.tictactoe.GridViewButton>
    <com.example.kasia.tictactoe.GridViewButton xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:scaleType="centerCrop"
        android:layout_gravity="fill_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        >
    </com.example.kasia.tictactoe.GridViewButton>
    <com.example.kasia.tictactoe.GridViewButton xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:layout_gravity="fill_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        >
    </com.example.kasia.tictactoe.GridViewButton>
    <com.example.kasia.tictactoe.GridViewButton xmlns:android="http://schemas.android.com/apk/res/android"
        android:scaleType="centerCrop"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"

        >
    </com.example.kasia.tictactoe.GridViewButton>
    <com.example.kasia.tictactoe.GridViewButton xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:scaleType="centerCrop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"

        >
    </com.example.kasia.tictactoe.GridViewButton>
    <com.example.kasia.tictactoe.GridViewButton xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:scaleType="centerCrop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"

        >
    </com.example.kasia.tictactoe.GridViewButton>

</com.example.kasia.tictactoe.MyGridLayout>
</android.support.constraint.ConstraintLayout>

public class MyGridLayout extends GridLayout {
public MyGridLayout(Context context) {
    super(context);
}

public MyGridLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyGridLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public MyGridLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec,widthMeasureSpec); 
}

}

public class GridViewButton extends Button {


public GridViewButton(Context context) {
    super(context);
}

public GridViewButton(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public GridViewButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public GridViewButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, widthMeasureSpec); 
}

}

我很感激任何提示......

1 个答案:

答案 0 :(得分:0)

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:weightSum="4" >

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher_main" />

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher_main"  />

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher_main"  />

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher_main"  />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:weightSum="4" >

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher_main" />

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher_main"  />

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher_main"  />

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher_main"  />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:weightSum="4" >

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher_main"  />

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher_main" />

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher_main"  />

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher_main"  />
</LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="4" >

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@mipmap/ic_launcher_main" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@mipmap/ic_launcher_main"  />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@mipmap/ic_launcher_main"  />
        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@mipmap/ic_launcher_main" />
    </LinearLayout>

</LinearLayout>