android-constraintlayout 相关问题

Android支持存储库中提供的一种新类型布局,构建于灵活约束系统之上,标记视图相对于彼此的位置。

android ConstraintLayout Flow:从右到左对齐项目

我正在使用 android ConstraintLayout Flow ,我必须从右到左排列项目,如下所示: XML代码: 我正在使用 android ConstraintLayout Flow ,我必须从右到左排列项目,如下所示: XML代码: <androidx.constraintlayout.helper.widget.Flow android:id="@+id/flow" android:layout_width="0dp" android:layout_height="wrap_content" app:flow_horizontalStyle="spread_inside" app:flow_maxElementsWrap="4" app:flow_verticalGap="29dp" app:flow_wrapMode="aligned" app:constraint_referenced_ids="item1 , item2 , ..." app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/headerTextView" /> 注意:我们的supportsRtl在清单中设置为False ... 我该怎么做?! 有一种解决方法,可以通过使用 ConstraintLayout 在 Y 方向上旋转 android:rotationY="180" 来从右向左将项目添加到流程中 这将反转整个布局,包括流程中的每个单独元素;因此,在下面的演示中,您也会看到相反的文本: 为了解决这个问题,我们必须对每个单独的元素进行相反的操作。这需要一个 ViewGroup,因此这里每个单独的元素都被包装在 FrameLayout 中以达到此目的。 演示: <?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="wrap_content" android:rotationY="180"> <androidx.constraintlayout.helper.widget.Flow android:id="@+id/flow" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" app:constraint_referenced_ids="button1, button2, button3, button4, button5, button6, button7, button8, button9" app:flow_horizontalGap="8dp" app:flow_maxElementsWrap="3" app:flow_verticalBias="1" app:flow_verticalGap="8dp" app:flow_verticalStyle="packed" app:flow_wrapMode="chain" tools:ignore="MissingConstraints" /> <FrameLayout android:id="@+id/button1" android:layout_width="0dp" android:layout_height="wrap_content" android:rotationY="180" tools:ignore="MissingConstraints"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_orange_light" android:text="1" android:textSize="18sp" /> </FrameLayout> <FrameLayout android:id="@+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:rotationY="180" tools:ignore="MissingConstraints"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_orange_light" android:text="2" android:textSize="18sp" /> </FrameLayout> <FrameLayout android:id="@+id/button3" android:layout_width="0dp" android:layout_height="wrap_content" android:rotationY="180" tools:ignore="MissingConstraints"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_orange_light" android:text="3" android:textSize="18sp" /> </FrameLayout> <FrameLayout android:id="@+id/button4" android:layout_width="0dp" android:layout_height="wrap_content" android:rotationY="180" tools:ignore="MissingConstraints"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_orange_light" android:text="4" android:textSize="18sp" /> </FrameLayout> <FrameLayout android:id="@+id/button5" android:layout_width="0dp" android:layout_height="wrap_content" android:rotationY="180" tools:ignore="MissingConstraints"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_orange_light" android:text="5" android:textSize="18sp" /> </FrameLayout> <FrameLayout android:id="@+id/button6" android:layout_width="0dp" android:layout_height="wrap_content" android:rotationY="180" tools:ignore="MissingConstraints"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_orange_light" android:text="6" android:textSize="18sp" /> </FrameLayout> <FrameLayout android:id="@+id/button7" android:layout_width="0dp" android:layout_height="wrap_content" android:rotationY="180" tools:ignore="MissingConstraints"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_orange_light" android:text="7" android:textSize="18sp" /> </FrameLayout> <FrameLayout android:id="@+id/button8" android:layout_width="0dp" android:layout_height="wrap_content" android:rotationY="180" tools:ignore="MissingConstraints"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_orange_light" android:text="8" android:textSize="18sp" /> </FrameLayout> <FrameLayout android:id="@+id/button9" android:layout_width="0dp" android:layout_height="wrap_content" android:rotationY="180" tools:ignore="MissingConstraints"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_orange_light" android:text="9" android:textSize="18sp" /> </FrameLayout> </androidx.constraintlayout.widget.ConstraintLayout> 我找到并且有效的解决方案是在父级(我们的 ConstraintLayout)上设置 RotationY=180f,然后在 ConstraintLayout 中的每个项目上设置 RotationY=180f。 app:flow_horizontalBias="1" 在缺陷布局中使用它 我正在使用流程布局并让它可以使用: 应用程序:flRtl =“true”>

回答 4 投票 0

如何在约束布局中实现动态textview调整大小?

我在约束布局中有两个文本视图,它们水平相邻: 我在约束布局中有两个文本视图,它们水平相邻: <?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="wrap_content" android:orientation="horizontal" android:paddingVertical="12dp"> <TextView android:id="@+id/textViewLeft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:maxLines="2" app:layout_constrainedWidth="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@id/textViewRight" app:layout_constraintHorizontal_chainStyle="spread_inside" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textViewRight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:ellipsize="end" android:maxLines="2" android:singleLine="false" app:layout_constrainedWidth="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@id/textViewLeft" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> 当我为第一个文本视图设置长文本,为第二个文本视图设置短文本时,一切正常,第一个文本视图中的文本换行到另一行并被省略。 但是当我在第二个文本视图中制作长文本时,它会破坏第一个文本视图 出了什么问题以及如何解决? 试试这个,我在 TextView 之间插入视图并将其对齐到中心。并相应地设置约束。您也可以使用指南/障碍来实现这一点。 <?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="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="12dp"> <TextView android:id="@+id/textViewLeft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:maxLines="2" app:layout_constrainedWidth="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@id/divider" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/divider" android:layout_width="1dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="@id/textViewLeft" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textViewRight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:ellipsize="end" android:maxLines="2" android:singleLine="false" app:layout_constrainedWidth="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@id/divider" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>

回答 1 投票 0

在 TextView 数组中,仅显示最后一个 TextView

我正在尝试显示 TextView 数组,但只显示最后一个 TextView。我已经检查过根据 i 值移动约束边距,发现它们不在一个 a 之上...

回答 2 投票 0

约束布局中带有 Textview 的动态按钮

目前我正在使用(学习使用)ConstraintLayout。现在我创建了一个简单的布局,每个按钮下每行有 3 个按钮,我放置了一个文本视图,目前我正在手动添加按钮...

回答 1 投票 0

如何防止RecyclerView覆盖其下方的视图?

这是我的布局: 这是我的布局: <androidx.appcompat.widget.LinearLayoutCompat 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="wrap_content" android:orientation="vertical" tools:context=".ScannerFragment"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.camera.view.PreviewView android:id="@+id/cameraPreview" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintDimensionRatio="16:9" app:layout_constraintTop_toTopOf="parent" /> <ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="@id/cameraPreview"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/saleList" android:layout_width="match_parent" android:layout_height="wrap_content" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" /> </ScrollView> <View android:id="@+id/filler" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@id/scrollView" app:layout_constraintVertical_weight="1" /> <androidx.appcompat.widget.LinearLayoutCompat android:id="@+id/total_widget" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="4dp" app:layout_constraintBottom_toBottomOf="parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/total" android:textSize="24sp" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <TextView android:id="@+id/sum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp" /> </androidx.appcompat.widget.LinearLayoutCompat> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.appcompat.widget.LinearLayoutCompat> 这就是它的样子: 请查看图像底部,RecyclerView 中的项目是否覆盖了“Total”文本。我希望 RecyclerView 最多扩展至“Total”文本的顶部。 问题是,即使 RecyclerView 为空,我也希望“Total”文本粘在广告上方的底部。因此我不能简单地以这种方式限制它。 我尝试了“填充”视图的各种约束,但这也无法正常工作。在某些情况下,添加到 RecyclerView 的项目会使 RecyclerView 从 CameraPreview 和“Total”文本之间的中间展开。只是最终覆盖了 CameraPreview 和“Total”文本... 是否有一种有意识的方法来实现我想要的布局,而无需无休止地摆弄它? 几件事: 1)Recyclerviews 内置了滚动功能。无需将其放入 ScrollView 中。 2)向RecyclerView添加constraint_bottom_toTopOf(在删除滚动视图之后)并将其限制在您不希望它重叠的任何视图之上(我假设是total_widget)

回答 1 投票 0

ConstraintLayout 损坏了吗?

这是我的活动布局: 这是我的活动布局: <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"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/appbar" app:layout_constraintTop_toTopOf="parent" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true"> <com.google.android.material.appbar.MaterialToolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" /> </com.google.android.material.appbar.AppBarLayout> <androidx.fragment.app.FragmentContainerView android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="wrap_content" app:defaultNavHost="true" app:layout_constraintBottom_toTopOf="@id/adView" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/appbar" app:navGraph="@navigation/nav_graph" /> <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="wrap_content" app:adUnitId="ca-app-pub-3940256099942544/9214589741" app:adSize="BANNER" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> 这是里面显示的一个Fragment: <androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" tools:context=".ScannerFragment"> <androidx.camera.view.PreviewView android:id="@+id/cameraPreview" android:layout_width="match_parent" android:layout_height="200dp"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/sale_list" android:layout_width="match_parent" android:layout_height="wrap_content"/> <View android:layout_width="match_parent" android:layout_height="0sp" android:layout_weight="1"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Total" android:textSize="24sp"/> </androidx.appcompat.widget.LinearLayoutCompat> 这就是它的样子: 看一下FragmentContainerView:位于应用栏下方和广告视图上方是有限制的。然而,该片段显然违反了这些约束并忽略了应用栏和 adView。 我做错了什么吗?或者ConstraintLayout只是坏了?如何实现我想要的布局? 我已经尝试将 FragmentContainerView 放入 LinearLayout 以及 LinearLayout 上的约束,但无法见证任何变化。 您的 FragmentContainer 的高度为 wrap_content,这意味着它不会遵守 constraintTop_toBottomOf(和类似)属性设置的约束。 使用0dp(MATCH_CONSTRAINT)作为其高度是关键。请阅读更多相关信息这里。 ...前两个的工作方式与其他布局类似。最后一个将以匹配设置的约束的方式调整小部件的大小。如果设置了边距,它们将被考虑到计算中。 <androidx.fragment.app.FragmentContainerView android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="0dp" app:defaultNavHost="true" app:layout_constraintBottom_toTopOf="@id/adView" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/appbar" app:navGraph="@navigation/nav_graph" />

回答 1 投票 0

Ellipsize 不适用于 XML 中的并排文本视图

模拟 尝试在约束布局中构建类似的东西。两个文本视图并排放置,太长,应该省略。将layout_width设置为0dp会拉伸

回答 1 投票 0

Jetpack Compose - MotionLayout

相当于什么: 相当于什么: <Transition motion:constraintSetEnd="@id/end" motion:constraintSetStart="@id/start" motion:duration="1500" motion:motionInterpolator="linear"> <OnSwipe motion:dragDirection="dragUp" motion:touchAnchorId="@id/view1" motion:touchAnchorSide="top" /> </Transition> 关于 Compose 的 MotionLayout? 我在这两个链接上都没有看到答案: https://github.com/androidx/constraintlayout/wiki/Introduction-to-MotionLayout-in-Compose https://github.com/androidx/constraintlayout/wiki/Compose-MotionLayout-JSON-Syntax 我只想在手动滚动时为场景设置动画,而不是像示例中那样通过单击按钮来动画。 谢谢! Compose 中的 MotionLayout 尚未内置支持。 Compose 具有滑动处理功能 一种方法如下所示: var componentHeight by remember { mutableStateOf(1000f) } val swipeableState = rememberSwipeableState("Bottom") val anchors = mapOf(0f to "Bottom", componentHeight to "Top") val mprogress = (swipeableState.offset.value / componentHeight) 然后将进度传递到 MotionLayout Composable 中。 MotionLayout(motionScene = "", progress = mprogress, ...) 这里是一个如何编码的示例。 从androidx.constraintlayout:constraintlayout-compose:1.1.0-alpha01开始,您现在可以在onSwipe中使用MotionScene设置进行交易: MotionScene { val anchorComposable = createRefFor("someId") val state1 = constraintSet { //some constraints } val state2 = constraintSet { //some constraints } transition(state1, state2, "default") { onSwipe = OnSwipe( anchor = anchorComposable, direction = SwipeDirection.Up, side = SwipeSide.Top, ) } } 将此场景传递给MotionLayout,您将获得所需的滑动效果。不幸的是,它们仍然没有 xml MotionLayout 的所有功能,即没有 TransitionListener,因此无法检查滑动是否开始或完成。另一个重要的缺点是所有可点击的可组合项都被排除在可滑动区域之外。

回答 2 投票 0

Android UpdateLayoutParams match_constraint 不匹配新的给定约束

我有一个正在尝试完成的任务:当字体比例配置更改时,以编程方式更改约束并更新布局。这里我在 recyclerview 上有以下代码

回答 1 投票 0

如何在 Android Studio 中让图像随不同屏幕尺寸缩放?

在我的代码中,我的应用程序运行良好,但当我使用不同的屏幕尺寸时,我遇到了问题。按钮排列正确,但图像根本不缩放。 这是我的 XML 文件 在我的代码中,我的应用程序运行良好,但当我使用不同的屏幕尺寸时,我遇到了问题。按钮排列正确,但图像根本无法缩放。 这是我的 XML 文件 <?xml version="1.0" encoding="utf-8"?> <ScrollView 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="wrap_content" android:layout_height="wrap_content" tools:ignore="SpeakableTextPresentCheck"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFC107" android:padding="40dp" tools:context=".ActivitiesActivity"> <!-- ImageView Declarations --> <ImageView android:id="@+id/alabama_music_hall_of_fame" android:layout_width="match_parent" android:layout_height="150dp" android:contentDescription="@string/alabama_music_hall_of_fame_logo" app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="@id/the_boiler_room" app:srcCompat="@drawable/activity_alabama_music_hall_of_fame_card" /> <Button android:id="@+id/alabama_music_hall_of_fame_FacebookButton" android:layout_width="0dp" android:layout_height="33dp" android:background="@android:color/transparent" app:layout_constraintBottom_toBottomOf="@id/alabama_music_hall_of_fame" app:layout_constraintStart_toStartOf="@id/alabama_music_hall_of_fame" app:layout_constraintEnd_toStartOf="@id/alabama_music_hall_of_fame_InstagramButton" app:layout_constraintHorizontal_chainStyle="spread_inside" /> <Button android:id="@+id/alabama_music_hall_of_fame_InstagramButton" android:layout_width="0dp" android:layout_height="33dp" android:background="@android:color/transparent" app:layout_constraintBottom_toBottomOf="@id/alabama_music_hall_of_fame" app:layout_constraintStart_toEndOf="@id/alabama_music_hall_of_fame_FacebookButton" app:layout_constraintEnd_toStartOf="@id/alabama_music_hall_of_fame_WebsiteButton" /> <Button android:id="@+id/alabama_music_hall_of_fame_WebsiteButton" android:layout_width="0dp" android:layout_height="33dp" android:background="@android:color/transparent" app:layout_constraintBottom_toBottomOf="@id/alabama_music_hall_of_fame" app:layout_constraintStart_toEndOf="@id/alabama_music_hall_of_fame_InstagramButton" app:layout_constraintEnd_toStartOf="@id/alabama_music_hall_of_fame_DirectionsButton" /> <Button android:id="@+id/alabama_music_hall_of_fame_DirectionsButton" android:layout_width="0dp" android:layout_height="33dp" android:background="@android:color/transparent" app:layout_constraintBottom_toBottomOf="@id/alabama_music_hall_of_fame" app:layout_constraintStart_toEndOf="@id/alabama_music_hall_of_fame_WebsiteButton" app:layout_constraintEnd_toEndOf="@id/alabama_music_hall_of_fame" /> <!-- The Boiler Room ImageView --> <ImageView android:id="@+id/the_boiler_room" android:layout_width="0dp" android:layout_height="150dp" android:layout_marginTop="10dp" android:contentDescription="@string/the_boiler_room_logo" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/alabama_music_hall_of_fame" app:srcCompat="@drawable/activity_the_boiler_room" /> <!-- Buttons under The Boiler Room ImageView --> <Button android:id="@+id/the_boiler_room_FacebookButton" android:layout_width="0dp" android:layout_height="33dp" android:background="@android:color/transparent" app:layout_constraintBottom_toBottomOf="@id/the_boiler_room" app:layout_constraintEnd_toStartOf="@id/the_boiler_room_InstagramButton" app:layout_constraintStart_toStartOf="@id/the_boiler_room" app:layout_constraintHorizontal_chainStyle="spread" /> <Button android:id="@+id/the_boiler_room_InstagramButton" android:layout_width="0dp" android:layout_height="33dp" android:background="@android:color/transparent" app:layout_constraintBottom_toBottomOf="@id/the_boiler_room" app:layout_constraintEnd_toStartOf="@id/the_boiler_room_WebsiteButton" app:layout_constraintStart_toEndOf="@id/the_boiler_room_FacebookButton" /> <Button android:id="@+id/the_boiler_room_WebsiteButton" android:layout_width="0dp" android:layout_height="33dp" android:background="@android:color/transparent" app:layout_constraintBottom_toBottomOf="@id/the_boiler_room" app:layout_constraintEnd_toStartOf="@id/the_boiler_room_DirectionsButton" app:layout_constraintStart_toEndOf="@id/the_boiler_room_InstagramButton" /> <Button android:id="@+id/the_boiler_room_DirectionsButton" android:layout_width="0dp" android:layout_height="33dp" android:background="@android:color/transparent" app:layout_constraintBottom_toBottomOf="@id/the_boiler_room" app:layout_constraintEnd_toEndOf="@id/the_boiler_room" app:layout_constraintStart_toEndOf="@id/the_boiler_room_WebsiteButton" /> <!-- The Escape Room ImageView --> <ImageView android:id="@+id/the_escape_room" android:layout_width="0dp" android:layout_height="150dp" android:layout_marginTop="10dp" android:contentDescription="@string/the_escape_room_logo" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/the_boiler_room" app:srcCompat="@drawable/activity_the_escape_room" /> <!-- Buttons under The Escape Room ImageView --> <Button android:id="@+id/the_escape_room_FacebookButton" android:layout_width="0dp" android:layout_height="33dp" android:background="@android:color/transparent" app:layout_constraintBottom_toBottomOf="@id/the_escape_room" app:layout_constraintEnd_toStartOf="@id/the_escape_room_InstagramButton" app:layout_constraintStart_toStartOf="@id/the_escape_room" app:layout_constraintHorizontal_chainStyle="spread" /> <Button android:id="@+id/the_escape_room_InstagramButton" android:layout_width="0dp" android:layout_height="33dp" android:background="@android:color/transparent" app:layout_constraintBottom_toBottomOf="@id/the_escape_room" app:layout_constraintEnd_toStartOf="@id/the_escape_room_WebsiteButton" app:layout_constraintStart_toEndOf="@id/the_escape_room_FacebookButton" /> <Button android:id="@+id/the_escape_room_WebsiteButton" android:layout_width="0dp" android:layout_height="33dp" android:background="@android:color/transparent" app:layout_constraintBottom_toBottomOf="@id/the_escape_room" app:layout_constraintEnd_toStartOf="@id/the_escape_room_DirectionsButton" app:layout_constraintStart_toEndOf="@id/the_escape_room_InstagramButton" /> <Button android:id="@+id/the_escape_room_DirectionsButton" android:layout_width="0dp" android:layout_height="33dp" android:background="@android:color/transparent" app:layout_constraintBottom_toBottomOf="@id/the_escape_room" app:layout_constraintEnd_toEndOf="@id/the_escape_room" app:layout_constraintStart_toEndOf="@id/the_escape_room_WebsiteButton" /> </androidx.constraintlayout.widget.ConstraintLayout> </ScrollView> 我尝试过在图像视图中设置不同的约束,我已经累了 android:layout_width="wrap_content" android:layout_height="150dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_height="150dp" 几乎我尝试过的任何不同配置,我觉得我缺少一些基本的东西。 我已经弄清楚了,我在 ScrollView 内部使用了 CardView,无论屏幕尺寸如何,图像都保持相同。我还为我的按钮使用了 LinerLayout,这样它们就会保留在图像的底部! `<?xml version="1.0" encoding="utf-8"?> <ScrollView 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:fillViewport="true" tools:ignore="SpeakableTextPresentCheck"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFC107" android:padding="40dp" tools:context=".ActivitiesActivity"> <androidx.cardview.widget.CardView android:id="@+id/alabama_music_hall_of_fame_card" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="8dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:cardCornerRadius="0dp" app:cardElevation="4dp"> <ImageView android:id="@+id/alabama_music_hall_of_fame" android:layout_width="match_parent" android:layout_height="200dp" android:src="@drawable/activity_alabama_music_hall_of_fame_card" android:scaleType="fitXY" android:contentDescription="@string/alabama_music_hall_of_fame_logo"/> <LinearLayout style="?android:attr/buttonBarStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="bottom" android:gravity="center_horizontal"> <Button android:id="@+id/alabama_music_hall_of_fame_FacebookButton" style="?android:attr/buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:contentDescription="@string/amhfFB" android:background="@android:color/transparent" tools:ignore="VisualLintButtonSize" /> <Button android:id="@+id/alabama_music_hall_of_fame_InstagramButton" style="?android:attr/buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:contentDescription="@string/amhfIG" android:background="@android:color/transparent" tools:ignore="VisualLintButtonSize" /> <Button android:id="@+id/alabama_music_hall_of_fame_WebsiteButton" style="?android:attr/buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:contentDescription="@string/amhfWeb" android:background="@android:color/transparent" /> <Button android:id="@+id/alabama_music_hall_of_fame_DirectionsButton" style="?android:attr/buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:contentDescription="@string/amhfDir" android:background="@android:color/transparent" tools:ignore="VisualLintButtonSize" /> </LinearLayout> </androidx.cardview.widget.CardView> <androidx.cardview.widget.CardView android:id="@+id/the_boiler_room_card" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="16dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/alabama_music_hall_of_fame_card" app:cardCornerRadius="0dp" app:cardElevation="4dp"> <ImageView android:id="@+id/the_boiler_room" android:layout_width="match_parent" android:layout_height="150dp" android:contentDescription="@string/the_boiler_room_logo" android:src="@drawable/activity_the_boiler_room" android:scaleType="fitXY"/> <LinearLayout style="?android:attr/buttonBarStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="bottom" android:gravity="center_horizontal"> <Button android:id="@+id/the_boiler_room_FacebookButton" style="?android:attr/buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:contentDescription="@string/tbrFB" android:background="@android:color/transparent" /> <Button android:id="@+id/the_boiler_room_InstagramButton" style="?android:attr/buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:contentDescription="@string/tbrIG" android:background="@android:color/transparent" /> <Button android:id="@+id/the_boiler_room_WebsiteButton" style="?android:attr/buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:contentDescription="@string/tbrWeb" android:background="@android:color/transparent" /> <Button android:id="@+id/the_boiler_room_DirectionsButton" style="?android:attr/buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:contentDescription="@string/tbrDir" android:background="@android:color/transparent"/> </LinearLayout> </androidx.cardview.widget.CardView> <androidx.cardview.widget.CardView android:id="@+id/the_escape_room_card" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="16dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/the_boiler_room_card" app:cardCornerRadius="0dp" app:cardElevation="4dp"> <ImageView android:id="@+id/activity_the_escape_room" android:layout_width="match_parent" android:layout_height="200dp" android:contentDescription="@string/the_escape_room_logo" android:src="@drawable/activity_the_escape_room" android:scaleType="fitXY"/> <LinearLayout style="?android:attr/buttonBarStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="bottom" android:gravity="center_horizontal"> <Button android:id="@+id/the_escape_room_FacebookButton" style="?android:attr/buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:contentDescription="@string/terFB" android:background="@android:color/transparent" tools:ignore="VisualLintButtonSize" /> <Button android:id="@+id/the_escape_room_InstagramButton" style="?android:attr/buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:contentDescription="@string/terIG" android:background="@android:color/transparent" /> <Button android:id="@+id/the_escape_room_WebsiteButton" style="?android:attr/buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:contentDescription="@string/terWeb" android:background="@android:color/transparent" /> <Button android:id="@+id/the_escape_room_DirectionsButton" style="?android:attr/buttonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:contentDescription="@string/terDir" android:background="@android:color/transparent"/> </LinearLayout> </androidx.cardview.widget.CardView> </androidx.constraintlayout.widget.ConstraintLayout> </ScrollView>`

回答 1 投票 0

约束布局使得依赖视图消失,UI 扭曲

我有下面的用户界面,在实时图像可见之前都很好 下面是 XML 布局 我有下面的 UI,直到 Live 图像可见 下面是 XML 布局 <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="@dimen/epg_item_container_width" android:layout_height="@dimen/epg_item_container_with_description_height" android:layout_margin="1dp" android:clickable="true" android:focusable="true" android:padding="10dp"> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/program_image" android:layout_width="150dp" android:layout_height="0dp" app:layout_constraintDimensionRatio="H,16:9" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:src="@drawable/epg_info_default_icon" /> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="0dp" android:layout_height="0dp" android:layout_marginStart="5dp" app:layout_constraintBottom_toBottomOf="@+id/program_image" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/program_image" app:layout_constraintTop_toTopOf="@+id/program_image"> <androidx.appcompat.widget.AppCompatTextView android:id="@+id/program_title" android:layout_width="0dp" android:layout_height="wrap_content" android:clickable="true" android:ellipsize="end" android:ems="2" android:focusable="false" android:gravity="start|top" android:maxLines="2" android:textColor="#d3d3d3" android:textSize="13sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:text="2023 Stanley Cup Final" /> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/imgLive" android:layout_width="42dp" android:layout_height="16dp" android:layout_marginTop="5dp" android:src="@drawable/epg_live" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/program_title" /> <androidx.appcompat.widget.AppCompatTextView android:id="@+id/program_description" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginStart="5dp" android:singleLine="true" android:textColor="#FFFFFF" android:textSize="10sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="@+id/imgLive" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/imgLive" app:layout_constraintTop_toTopOf="@+id/imgLive" tools:text="| US-TV-PG" /> <androidx.appcompat.widget.AppCompatTextView android:id="@+id/synopsis" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:clickable="true" android:ellipsize="end" android:ems="2" android:maxLines="2" android:textColor="#d3d3d3" android:textSize="13sp" android:textStyle="bold" app:layout_constraintEnd_toStartOf="@+id/menu" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/imgLive" tools:text="Extended game replays from the 2023 Stanley Cup Final." /> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/menu" android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/epg_info_default_icon" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout> 现在,如果我将 gone 的可见性更改为 imgLive,UI 就会扭曲,并且 program_description 与 program_title 重叠 附上参考资料gone 您可以将 imgLive 和 programDescription 包装在一个简单的 LinearLayout 中,如下所示: <?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="@dimen/epg_item_container_width" android:layout_height="@dimen/epg_item_container_with_description_height" android:layout_margin="1dp" android:clickable="true" android:focusable="true" android:padding="10dp"> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/program_image" android:layout_width="150dp" android:layout_height="0dp" app:layout_constraintDimensionRatio="H,16:9" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:src="@drawable/epg_info_default_icon" /> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="0dp" android:layout_height="0dp" android:layout_marginStart="5dp" app:layout_constraintBottom_toBottomOf="@+id/program_image" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/program_image" app:layout_constraintTop_toTopOf="@+id/program_image"> <androidx.appcompat.widget.AppCompatTextView android:id="@+id/program_title" android:layout_width="0dp" android:layout_height="wrap_content" android:clickable="true" android:ellipsize="end" android:ems="2" android:focusable="false" android:gravity="start|top" android:maxLines="2" android:textColor="#d3d3d3" android:textSize="13sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:text="2023 Stanley Cup Final" /> <LinearLayout android:id="@+id/group" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/program_title" app:layout_constraintBottom_toTopOf="@id/synopsis" android:orientation="horizontal"> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/imgLive" android:layout_width="42dp" android:layout_height="16dp" android:layout_marginEnd="5dp" android:src="@drawable/epg_live"/> <androidx.appcompat.widget.AppCompatTextView android:id="@+id/program_description" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:singleLine="true" android:textColor="#FFFFFF" android:textSize="10sp" android:textStyle="bold" tools:text="| US-TV-PG" /> </LinearLayout> <androidx.appcompat.widget.AppCompatTextView android:id="@+id/synopsis" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:clickable="true" android:ellipsize="end" android:ems="2" android:maxLines="2" android:textColor="#d3d3d3" android:textSize="13sp" android:textStyle="bold" app:layout_constraintEnd_toStartOf="@+id/menu" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/group" tools:text="Extended game replays from the 2023 Stanley Cup Final." /> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/menu" android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/epg_info_default_icon" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

回答 1 投票 0

约束布局中的前景重叠

我有两个约束布局嵌套在另一个约束布局中。我已经为父约束布局设置了前景图像,但图像与其他两个布局重叠,正如您在即时消息中看到的...

回答 1 投票 0

如何以编程方式连接约束布局

我将为 Portal 模式和横向屏幕创建一个自适应 Android Activity,如下所示 内容的 ConstraintLayout(蓝光背景色),带有 android:id="@+id/

回答 1 投票 0

在运行时设置约束集未与父级的末端正确对齐

我在 xml 中有以下代码片段。它只是显示 3 种不同的视图。但在运行时我想改变对齐方式。但是,textviewShopNow 应在父端对齐。但有...

回答 1 投票 0

在底部对齐 RecyclerView 中的项目

我在 ConstraintLayout 中有一个 RecyclerView,其顶部从 textView 的底部开始,其底部向下延伸到父级。我希望此 RecyclerView 中的项目默认为 ...

回答 3 投票 0

在文本视图与可见性对齐后对齐文本视图消失了

当小图标的优惠券消失后,布局中的这部分 当小图标的优惠券消失后,布局中的这部分 <androidx.appcompat.widget.AppCompatImageView android:id="@+id/imageViewCoupon" android:layout_width="14dp" android:layout_height="14dp" android:layout_marginStart="12dp" android:layout_marginTop="1dp" android:scaleType="fitXY" android:src="@drawable/icon_coupon" app:layout_constraintStart_toEndOf="@+id/imageView" app:layout_constraintTop_toBottomOf="@+id/textViewTitle" tools:visibility="gone"/> 我画的绿色框应该是描述应该对齐的地方。 我尝试过使用屏障,但可以让它发挥作用 我有以下布局 <?xml version="1.0" encoding="utf-8"?> <com.google.android.material.card.MaterialCardView xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:elevation="@dimen/margin_0dp" app:cardElevation="0dp" android:layout_height="wrap_content"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/layoutCartItem" android:layout_width="match_parent" android:layout_height="72dp"> <View android:id="@+id/highlineView" android:layout_width="@dimen/margin_6dp" android:layout_height="match_parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" android:background="@color/yellowHighlineColor" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideLineDisplayDateVertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent="0.13"/> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideLineDisplayDateHorizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent="0.5"/> <com.google.android.material.textview.MaterialTextView android:id="@+id/textViewDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:includeFontPadding="false" android:gravity="center_vertical|end" android:lineSpacingExtra="@dimen/order_label_lines_pace_extra" android:textStyle="bold" android:maxLength="2" android:layout_marginBottom="-6dp" android:textColor="@color/blackSectionTitle" android:textSize="@dimen/text_size_20sp" app:fontFamily="@font/ibmp_lexsansthai_semi" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="@id/guideLineDisplayDateVertical" app:layout_constraintBottom_toTopOf="@id/guideLineDisplayDateHorizontal" tools:text="12" /> <com.google.android.material.textview.MaterialTextView android:id="@+id/textViewDay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:includeFontPadding="false" android:lineSpacingExtra="@dimen/order_label_lines_pace_extra" android:textColor="@color/blackSectionTitle" android:textSize="@dimen/text_size_12sp" android:maxLength="3" app:fontFamily="@font/ibmp_lexsansthai_regular" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="@id/guideLineDisplayDateVertical" app:layout_constraintTop_toBottomOf="@id/guideLineDisplayDateHorizontal" tools:text="Mon" /> <com.airbnb.lottie.LottieAnimationView android:id="@+id/imageView" android:layout_width="@dimen/margin_44dp" android:layout_height="@dimen/margin_44dp" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="@id/guideLineDisplayDateVertical" app:layout_constraintTop_toTopOf="parent" tools:src="@tools:sample/avatars" /> <com.google.android.material.textview.MaterialTextView android:id="@+id/textViewTitle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="12dp" android:layout_marginTop="@dimen/margin_12dp" android:ellipsize="end" android:textSize="14sp" android:textStyle="bold" android:gravity="start" android:maxLines="1" android:textColor="@color/blackSectionTitle" app:fontFamily="@font/ibmp_lexsansthai_semi" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toEndOf="@+id/imageView" app:layout_constraintEnd_toStartOf="@id/imageViewChevron" app:layout_constraintTop_toTopOf="parent" tools:text="This is the title" /> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/imageViewCoupon" android:layout_width="14dp" android:layout_height="14dp" android:layout_marginStart="12dp" android:layout_marginTop="1dp" android:scaleType="fitXY" android:src="@drawable/icon_coupon" app:layout_constraintStart_toEndOf="@+id/imageView" app:layout_constraintTop_toBottomOf="@+id/textViewTitle" tools:visibility="visible"/> <com.google.android.material.textview.MaterialTextView android:id="@+id/textViewDescription" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="-3dp" android:textSize="12sp" android:ellipsize="end" android:layout_marginStart="4dp" android:layout_marginBottom="14dp" android:gravity="center_vertical|start" android:lineSpacingExtra="@dimen/order_label_lines_pace_extra" android:maxLines="1" android:textColor="#FF3D1C" app:fontFamily="@font/ibmp_lexsansthai_regular" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@+id/imageViewCoupon" app:layout_constraintEnd_toStartOf="@id/imageViewChevron" app:layout_constraintTop_toBottomOf="@+id/textViewTitle" tools:text="This is the description" /> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/imageViewChevron" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginEnd="12dp" android:scaleType="fitXY" app:layout_constraintHorizontal_bias="1" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:src="@drawable/icons_chevron_right" /> </androidx.constraintlayout.widget.ConstraintLayout> </com.google.android.material.card.MaterialCardView> 您必须进行两项更改,一是删除起始边距,二是更改起始约束。 <androidx.appcompat.widget.AppCompatImageView android:id="@+id/imageViewCoupon" android:layout_width="14dp" android:layout_height="14dp" android:layout_marginTop="1dp" android:scaleType="fitXY" android:src="@drawable/icon_coupon" app:layout_constraintStart_toStartOf="@+id/textViewTitle" app:layout_constraintTop_toBottomOf="@+id/textViewTitle" tools:visibility="visible"/>

回答 1 投票 0

Android - 将覆盖或浮动视图放置在布局中另一个视图之上

在 Android 应用程序中,我有一个布局,其中顶部包含一个 TextView,其下方包含两个元素:一个图标和另一个 TextView。第二个 TextView 显示一个经过

回答 1 投票 0

如何在 ConstraintLayout 中使子视图居中但保持宽高比

我正在使用包含子视图的ConstraintLayout。 我希望这个子视图居中但保持纵横比。 布局(横向)变体: 我正在使用包含子视图的ConstraintLayout。 我希望这个子视图居中,但保持纵横比。 布局(横向)变体: <?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" android:id="@+id/previewParent" tools:context=".PeakerActivity"> <androidx.appcompat.widget.LinearLayoutCompat android:layout_width="0px" android:layout_height="0px" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintDimensionRatio="16:9" android:background="@color/black" android:gravity="center" android:id="@+id/flexbox2"> </androidx.appcompat.widget.LinearLayoutCompat> </androidx.constraintlayout.widget.ConstraintLayout> 添加android:gravity="center"没有效果,子视图仍然不居中。 编辑:添加图像,如您所见,左侧紫色边大于右侧。 要将子视图在 ConstraintLayout 中居中,同时保持其纵横比,您可以将其包装在另一个布局中,并将约束应用于该包装器布局。以下是修改布局的方法: <?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:id="@+id/previewParent" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".PeakerActivity"> <androidx.appcompat.widget.LinearLayoutCompat android:id="@+id/wrapperLayout" android:layout_width="0px" android:layout_height="0px" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.appcompat.widget.LinearLayoutCompat android:layout_width="0dp" android:layout_height="0dp" android:gravity="center" app:layout_constraintDimensionRatio="16:9" android:background="@color/black"> <!-- Your child views here --> </androidx.appcompat.widget.LinearLayoutCompat> </androidx.appcompat.widget.LinearLayoutCompat> </androidx.constraintlayout.widget.ConstraintLayout> 在此修改后的布局中,我将子视图包装在另一个 LinearLayoutCompat 中,并将宽高比约束应用于此包装器布局。然后,我使用重力属性将子视图在此包装器布局中居中。这应该使子视图居中,同时保持其纵横比。

回答 1 投票 0

Android中如何将图标垂直对齐到textview第一行的中心

我想放置左侧图标(请参阅下图),垂直居中于 TextView 中文本的第一行。我找不到方法,我在文本视图、重力和约束上尝试了drawableLeft...

回答 2 投票 0

有没有办法去除ScrollView中屏幕顶部和图像顶部之间的空间?

我想显示我的超高图像,所以我尝试将其放入ScrollView中,并将其限制在屏幕顶部,以便可以滚动它。我想象它会填补

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.