Java 如何使3个图像按钮响应?
我正在努力实现这样的目标: 占据整个屏幕的三个图像按钮菜单, 其中三个必须是等高,宽度将是设备的,我很难做到这一点,我通过一个约束调整使其“响应”,但根据设备大小,会出现间隙,不确定原因: 这是我的布局:Java 如何使3个图像按钮响应?,java,android,android-imagebutton,Java,Android,Android Imagebutton,我正在努力实现这样的目标: 占据整个屏幕的三个图像按钮菜单, 其中三个必须是等高,宽度将是设备的,我很难做到这一点,我通过一个约束调整使其“响应”,但根据设备大小,会出现间隙,不确定原因: 这是我的布局: <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.androi
<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"
android:background="#921B1F33"
tools:context="com.example.minacar.MainActivity">
<ImageButton
android:id="@+id/transferButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@string/descripcion_button_1"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/menutransfer"
/>
<ImageButton
android:id="@+id/rentaCarButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@string/descripcion_button_2"
android:scaleType="fitXY"
app:layout_constraintBottom_toTopOf="@+id/transferButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/menurent" />
<ImageButton
android:id="@+id/carButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@string/descripcion_button_3"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/transferButton"
app:srcCompat="@drawable/menucar" />
任何帮助或指导将不胜感激,提前感谢
不确定是否有用,我使用的图像是三个大小相同的图像,1417x929 PNG。我建议在这种情况下使用LinerLayout,将每个元素的权重设置为1,匹配父元素的宽度将达到所需的行为
<LinerLayout android:orientation = "vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#921B1F33"
tools:context="com.example.minacar.MainActivity">
<ImageButton
android:id="@+id/carButton"
android:layout_width="match_parent"
android:layout_height="0dp"
android:weight = "1" />
<ImageButton
android:id="@+id/carButton"
android:layout_width="match_parent"
android:layout_height="0dp"
android:weight = "1" />
<ImageButton
android:id="@+id/carButton"
android:layout_width="match_parent"
android:layout_height="0dp"
android:weight = "1" />
<LinerLayout>
您可以将链条与以下部件一起使用:
app:layout_constraintVertical_chainStyle="spread"
请参阅:在布局中使用垂直链,并通过将所有四个(顶部、底部、开始、结束)设置为0,确保图像按钮与约束匹配。中间的空格来自
ImageButton
的默认填充。为防止出现这种情况,请为所有这些图像添加透明背景
<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"
android:background="#921B1F33">
<ImageButton
android:id="@+id/transferButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
app:layout_constraintBottom_toTopOf="@id/carButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/rentaCarButton"
app:srcCompat="@drawable/menutransfer"
android:background="@color/transparent"
/>
<ImageButton
android:id="@+id/rentaCarButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
app:layout_constraintBottom_toTopOf="@+id/transferButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
app:srcCompat="@drawable/menurent"
android:background="@color/transparent"/>
<ImageButton
android:id="@+id/carButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/transferButton"
app:srcCompat="@drawable/menucar"
android:background="@color/transparent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
太棒了!我不知道,但我还是有差距?有办法解决这个问题吗?这可能是因为图像没有占据整个空间。我甚至不知道该怎么处理它,试着拉伸图像,让它占据所有的空闲空间。看一看可能会有帮助:谢谢,这就行了!