gpt4 book ai didi

android - 使用约束布局等距四张图像

转载 作者:行者123 更新时间:2023-12-04 08:45:54 25 4
gpt4 key购买 nike

我想使用约束布局等距四张图像......我使用了指导方针,但它给了我很多图像之间的空间,并且右角图像超出了屏幕
我想要这样的东西-->
enter image description here
但是从下面的代码我得到了这个 -->
enter image description here
从另一个设备我尝试运行它没有给出预期的结果
以下是代码:--

<androidx.drawerlayout.widget.DrawerLayout 
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">


<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/holder"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent"
android:orientation="vertical">


<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"
app:titleTextColor="#ffffff"
app:title="@string/app_name"
android:background="@color/colorPrimary"
tools:ignore="MissingConstraints" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cvcxv"
android:layout_below="@id/toolbar"
app:layout_constraintTop_toBottomOf="@id/toolbar"


tools:ignore="MissingConstraints">
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="687px"
android:id="@+id/viewpagerhome"
tools:ignore="MissingConstraints" />

<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="-30dp"
android:layout_marginBottom="-3dp"
android:gravity="bottom"
app:layout_constraintBottom_toBottomOf="parent"
android:padding="10dip"
app:centered="true"
app:fillColor="@color/colorPrimary"
app:pageColor="#FFFFFF"
app:snap="false"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:gravity="bottom"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent">

<Button
android:id="@+id/image1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:background="@drawable/chairsicon"
app:layout_constraintDimensionRatio="h,1:1"
app:layout_constraintEnd_toStartOf="@id/image2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/image2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:background="@drawable/chairsicon"
app:layout_constraintDimensionRatio="h,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/image1"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/image3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:background="@drawable/chairsicon"
app:layout_constraintDimensionRatio="h,1:1"
app:layout_constraintEnd_toStartOf="@id/image4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/image1" />

<Button
android:id="@+id/image4"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="8dp"
android:background="@drawable/chairsicon"
app:layout_constraintDimensionRatio="h,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/image3"
app:layout_constraintTop_toBottomOf="@id/image2" />

</androidx.constraintlayout.widget.ConstraintLayout>



</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>


<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:layout_gravity="start"
app:itemTextColor="#ffffff"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/menu"
app:itemIconTint="#ffffff"
app:itemBackground="@drawable/drawer_item_bg"
/>

</drawer>
不同的设备提供不同的输出但不需要一个(第一张图片)
需要帮助才能适合所有设备(小设备到大设备)

最佳答案

不确定您对问题的确切要求,但我可以看到您需要在设备上显示 4 个宽度和高度相等的正方形。
您不需要为垂直和水平使用指南,而是垂直分布它,您可以使用屏障来正确对齐它们。
查看下面的代码:

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.viewpager.widget.ViewPager
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
app:layout_constraintBottom_toTopOf="@id/image_divider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/vertical_divider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent=".5" />

<androidx.constraintlayout.widget.Barrier
android:id="@+id/image_divider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="top"
app:constraint_referenced_ids="view1, view2" />

<androidx.constraintlayout.widget.Barrier
android:id="@+id/horizontal_divider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="top"
app:constraint_referenced_ids="view3, view4" />

<View
android:id="@+id/view1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
android:background="@android:color/holo_orange_dark"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@id/vertical_divider"
app:layout_constraintStart_toStartOf="parent" />

<View
android:id="@+id/view2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
android:background="@android:color/holo_orange_light"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/vertical_divider" />

<View
android:id="@+id/view3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
android:background="@android:color/holo_red_light"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@id/vertical_divider"
app:layout_constraintStart_toStartOf="parent" />

<View
android:id="@+id/view4"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
android:background="@android:color/holo_red_dark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/vertical_divider" />
</androidx.constraintlayout.widget.ConstraintLayout>
输出:
Output of code
(这将在不同的屏幕尺寸上保持一致)
Different screen sizes
这里,垂直指南用于在水平方向上均匀分布 View ,而屏障用于在垂直方向对齐两个 View 。
尺寸比例用于使正方形与 0dp保持一致作为高度和宽度。
编辑: O.P. 寻求在底部用方形框排列整个 View 层次结构的解决方案,下面给出了完整的代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="@string/app_name"
app:titleTextColor="#ffffff"
tools:ignore="MissingConstraints" />

<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpagerhome"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
app:layout_constraintBottom_toTopOf="@id/image_divider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
tools:ignore="NotSibling" />

<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="-30dp"
android:layout_marginBottom="-3dp"
android:gravity="bottom"
android:padding="10dip"
app:centered="true"
app:fillColor="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="@id/viewpagerhome"
app:layout_constraintEnd_toEndOf="@id/viewpagerhome"
app:layout_constraintStart_toStartOf="@id/viewpagerhome"
app:pageColor="#FFFFFF"
app:snap="false" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/vertical_divider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent=".5" />

<androidx.constraintlayout.widget.Barrier
android:id="@+id/image_divider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="top"
app:constraint_referenced_ids="view1, view2" />

<androidx.constraintlayout.widget.Barrier
android:id="@+id/horizontal_divider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="top"
app:constraint_referenced_ids="view3, view4" />

<View
android:id="@+id/view1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
android:background="@drawable/tableicon"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@id/vertical_divider"
app:layout_constraintStart_toStartOf="parent" />

<View
android:id="@+id/view2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
android:background="@drawable/sofaicon"
app:layout_constraintBottom_toTopOf="@id/horizontal_divider"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/vertical_divider" />

<View
android:id="@+id/view3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
android:background="@drawable/chairsicon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="@id/vertical_divider"
app:layout_constraintStart_toStartOf="parent" />

<View
android:id="@+id/view4"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
android:background="@drawable/cupboardicon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/vertical_divider" />
</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/transparent"
app:headerLayout="@layout/nav_header_main"
app:itemBackground="@drawable/drawer_item_bg"
app:itemIconTint="#ffffff"
app:itemTextColor="#ffffff"
app:menu="@menu/menu" />

</androidx.drawerlayout.widget.DrawerLayout>

关于android - 使用约束布局等距四张图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64332067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com