gpt4 book ai didi

android - 如何在约束布局中动态设置权重

转载 作者:太空狗 更新时间:2023-10-29 14:35:34 25 4
gpt4 key购买 nike

我需要动态地实现以下布局的等价物。我尝试过的代码适用于位置,元素按我想要的方式对齐,但权重值不会改变任何东西。我还没有想出如何让搜索栏缩小其宽度,以便为切换按钮提供足够的空间,而不是将其推到一边。

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 View :

<!-- 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" />

我需要修改什么来强制搜索栏占用开关旁边的剩余空间而不是插入它?

最佳答案

代码几乎是正确的。您没有为 SeekBar 指定开始约束,因此它会与其他所有内容一起消失。尝试以下操作:

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(createLayout())
supportActionBar?.setTitle("")

}

fun createLayout(): ConstraintLayout {

val layout = ConstraintLayout(this).apply {
id = View.generateViewId()
layoutParams = ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
)
}

val dimLevelView = LayoutInflater.from(this).inflate(R.layout.seekbar_widget, layout, false)
val onView = LayoutInflater.from(this).inflate(R.layout.switch_widget, layout, false)

layout.addView(dimLevelView)
layout.addView(onView)

val set = ConstraintSet()
set.clone(layout)

set.connect(
dimLevelView.id,
ConstraintSet.START,
ConstraintSet.PARENT_ID,
ConstraintSet.START
)

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)
return layout
}
}

关于android - 如何在约束布局中动态设置权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56751886/

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