Android ConstraintLayout – Top constraint for two layout
我被 ConstraintLayout 卡住了,并且对使用哪个属性来做我想做的事情感到困惑。
根据下图,我想根据视图的高度将
场景:
如果我将详细视图的顶部约束作为 OrderTakenBy TextView 的底部,如果它获得更多行,它将与 OrderCollectedBy Textview 的视图重叠(如下图)。反之亦然。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".activity.AccountMasterAddActivity" tools:showIn="@layout/activity_account_master_add"> <TextView <TextView <TextView <TextView <TextView <TextView <TextView <TextView <TextView <TextView <TextView <TextView <TextView <androidx.constraintlayout.widget.Guideline <androidx.recyclerview.widget.RecyclerView </androidx.constraintlayout.widget.ConstraintLayout> |
有人可以帮忙吗?
您可以使用屏障来克服这个问题。
Barrier 引用多个小部件作为输入,并根据指定侧的最极端小部件创建虚拟指南。例如,左屏障将与所有引用视图的左侧对齐。
这里是屏障的文档
将接受的订单或收集的订单添加为
XML 参考:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<androidx.constraintlayout.widget.Barrier android:id="@+id/barrier" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginEnd="411dp" app:barrierDirection="bottom" app:constraint_referenced_ids="order_taken_by,order_collected_by" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" tools:layout_editor_absoluteY="21dp"/> <androidx.appcompat.widget.AppCompatTextView |
这是输出:
Use bottom barrier
Use Deatils textview top constraint to barrier and barrier reference to OrderTakenBy, OrderCollectedBy textview.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 <?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"><TextView
android:id="@+id/OrderTakenBy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="warehouse sdgjklsdj jgkjskg"
app:layout_constraintEnd_toStartOf="@+id/OrderCollectedBy"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /><TextView
android:id="@+id/OrderCollectedBy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="hospital fhkhsf"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/OrderTakenBy"
app:layout_constraintTop_toTopOf="parent" /><androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="bottom"
app:constraint_referenced_ids="OrderCollectedBy, OrderTakenBy" /><TextView
android:id="@+id/Details"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:text="lorem_ipsum"
android:gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/barrier" /></androidx.constraintlayout.widget.ConstraintLayout>
我认为您可以将
order taken by 和order collected by package在另一个constraint layout 中,或者以编程方式检查TextView’s 的高度(在设置文本之后)并以编程方式更改顶部约束。