我需要动态实现以下布局的等效项。我尝试过的代码可以正确地用于位置,元素可以按照我的要求对齐,但是权重值不会发生任何变化。我还没有弄清楚如何使搜索栏缩小其宽度以给切换按钮留出足够的空间,而不是侧按它。
相当于我要动态编码的Xml:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<no.beiningbogen.snute.common.view.TouchAwareSeekBar
android:id="@+id/touchAwareSeekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:dsb_indicatorColor="@color/discrete_seek_bar"
app:dsb_progressColor="@color/discrete_seek_bar"
app:dsb_scrubberHeight="5dp"
app:dsb_thumbSize="20dp"
app:dsb_trackColor="@color/colorMaterialGray300"
app:dsb_trackHeight="5dp"
app:layout_constraintEnd_toStartOf="@+id/device_switch"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/device_switch"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="@null"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/touchAwareSeekBar"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我实现的代码:
val layout = ConstraintLayout(context).apply {
id = View.generateViewId()
layoutParams = ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
)
}
val dimLevelView = map[DeviceAttributeType.dimLevel]
val onView = map[DeviceAttributeType.on]
layout.addView(dimLevelView)
layout.addView(onOffView)
val set = ConstraintSet()
set.clone(layout)
set.connect(dimLevelView.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
set.connect(dimLevelView.id, ConstraintSet.END, onView.id, ConstraintSet.START)
set.connect(dimLevelView.id, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP)
set.centerVertically(dimLevelView.id, ConstraintSet.PARENT_ID)
set.setHorizontalWeight(dimLevelView.id, 1.toFloat())
set.connect(onView.id, ConstraintSet.START, dimLevelView.id, ConstraintSet.END)
set.connect(onView.id, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)
set.connect(onView.id, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP)
set.centerVertically(onView.id, ConstraintSet.PARENT_ID)
set.applyTo(layout)
我从xml获得了dim和onOff视图:
<!-- one file to define this -->
<no.beiningbogen.snute.common.view.TouchAwareSeekBar
android:id="@+id/touchAwareSeekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:dsb_indicatorColor="@color/discrete_seek_bar"
app:dsb_progressColor="@color/discrete_seek_bar"
app:dsb_scrubberHeight="5dp"
app:dsb_thumbSize="20dp"
app:dsb_trackColor="@color/colorMaterialGray300"
app:dsb_trackHeight="5dp" />
<!-- in another file -->
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/device_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null" />
我需要修改些什么,以迫使搜索栏占据开关旁边的剩余空间而不是推动开关?
答案 0 :(得分:0)
该代码几乎是正确的。您没有为 SeekBar 指定起始约束,因此它与其他所有约束都消失了。请尝试以下操作:
overflow:hidden