我将图像约束到xml文件中的wrap_content后,我希望imageview的边界仅覆盖屏幕的图像,而不会在android studio中扩展到屏幕上。
1.首先,我添加了一个网格图像。2.我添加了一个垂直布局,该布局应与图像大小相同。3.我在上述垂直布局中添加了3个水平布局。4.每个水平布局均包含3个图像视图。
上面提到的步骤是创建3X3网格。我限制了步骤2中提到的垂直网格,使其完全适合3X3网格的imageview。
这3个水平布局包含3个imageview,它们实际上是O-X游戏的图块。
问题是要知道如何使grid_image布局完全适合我所拥有的网格的图像。请避免手动调整布局大小,因为这会在横向模式屏幕上产生问题
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<ImageView
android:id="@+id/imageView_grid3X3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/tic_tac_toe" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="@+id/imageView_grid3X3"
app:layout_constraintEnd_toEndOf="@+id/imageView_grid3X3"
app:layout_constraintStart_toStartOf="@+id/imageView_grid3X3"
app:layout_constraintTop_toTopOf="@+id/imageView_grid3X3">
<LinearLayout
android:id="@+id/layout0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="top"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="player_tap"
android:padding="20dp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="player_tap"
android:padding="20dp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="player_tap"
android:padding="20dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="player_tap"
android:padding="20dp" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="player_tap"
android:padding="20dp" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="player_tap"
android:padding="20dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="bottom"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="player_tap"
android:padding="20dp" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="player_tap"
android:padding="20dp" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="player_tap"
android:padding="20dp" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
你能解释一下你想要什么吗?