我制作了gridLayout,其列数和行数均设置为3。当我复制并粘贴X的代码(我要打井字游戏)时,它会溢出并且不会更改行,而在模拟器中它将在下一行显示它。
我应该怎么做才能使其在设计页面中显示?
XML代码是:
<?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=".MainActivity">
<android.support.v7.widget.GridLayout
android:id="@+id/gridLayout"
android:layout_width="368dp"
android:layout_height="368dp"
android:layout_margin="8dp"
android:background="@drawable/board"
app:columnCount=" 3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:rowCount=" 3">
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height=" 100dp"
android:layout_margin=" 8dp"
app:srcCompat="@drawable/x" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="100dp"
android:layout_height=" 100dp"
android:layout_margin=" 8dp"
app:srcCompat="@drawable/x" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="100dp"
android:layout_height=" 100dp"
android:layout_margin=" 8dp"
app:srcCompat="@drawable/x" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="100dp"
android:layout_height=" 100dp"
android:layout_margin=" 8dp"
app:srcCompat="@drawable/x" />
</android.support.v7.widget.GridLayout>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:1)
使用边距使GridLayout居中 GridLayout margin reference from google android deveelopers
<?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=".MainActivity">
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="368dp"
android:layout_height="368dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/board"
app:columnCount=" 3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:rowCount=" 3">
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height=" 100dp"
android:layout_margin=" 8dp"
app:srcCompat="@drawable/x" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="100dp"
android:layout_height=" 100dp"
android:layout_margin=" 8dp"
app:srcCompat="@drawable/x" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="100dp"
android:layout_height=" 100dp"
android:layout_margin=" 8dp"
app:srcCompat="@drawable/x" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="100dp"
android:layout_height=" 100dp"
android:layout_margin=" 8dp"
app:srcCompat="@drawable/x" />
</GridLayout>
`
答案 1 :(得分:0)
将app:layout_gravity="center"
添加到每个ImageView
<?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=".MainActivity">
<android.support.v7.widget.GridLayout
android:id="@+id/gridLayout"
android:layout_width="368dp"
android:layout_height="368dp"
android:layout_margin="8dp"
android:background="@drawable/ic_launcher_background"
app:columnCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:rowCount="3">
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="8dp"
app:layout_gravity="center"
app:srcCompat="@drawable/ic_android" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="8dp"
app:layout_gravity="center"
app:srcCompat="@drawable/ic_android" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="8dp"
app:layout_gravity="center"
app:srcCompat="@drawable/ic_android" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="8dp"
app:layout_gravity="center"
app:srcCompat="@drawable/ic_android" />
</android.support.v7.widget.GridLayout>
</android.support.constraint.ConstraintLayout>
答案 2 :(得分:0)
无需使用GridLayout
就可以获取所需的输出。您只能使用ContraintLayout
。
尝试一下
<?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=".MainActivity">
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/app_name"
app:layout_constraintEnd_toStartOf="@+id/imageView2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_home_black_24dp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/app_name"
app:layout_constraintEnd_toStartOf="@+id/imageView3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/imageView1"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_home_black_24dp" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/app_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_home_black_24dp" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/app_name"
app:layout_constraintEnd_toStartOf="@+id/imageView5"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView1"
app:srcCompat="@drawable/ic_home_black_24dp" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/app_name"
app:layout_constraintEnd_toStartOf="@+id/imageView6"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/imageView4"
app:layout_constraintTop_toBottomOf="@+id/imageView2"
app:srcCompat="@drawable/ic_home_black_24dp" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/app_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/imageView5"
app:layout_constraintTop_toBottomOf="@+id/imageView3"
app:srcCompat="@drawable/ic_home_black_24dp" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageView8"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView4"
app:srcCompat="@drawable/ic_home_black_24dp" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageView9"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/imageView7"
app:layout_constraintTop_toBottomOf="@+id/imageView5"
app:srcCompat="@drawable/ic_home_black_24dp" />
<ImageView
android:id="@+id/imageView9"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/imageView8"
app:layout_constraintTop_toBottomOf="@+id/imageView6"
app:srcCompat="@drawable/ic_home_black_24dp" />
</android.support.constraint.ConstraintLayout>