我正在尝试以编程方式更改 Kotlin 中线性布局的边距,如下所示。但是当我点击页面时应用程序崩溃了。构建过程中没有错误。 我在这里搜索但找不到有效的解决方案。希望能在这里得到答案。谢谢!
class SettingsFragment : Fragment() {
private lateinit var linearLayout: LinearLayout
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_settings, container, false)
linearLayout = view.findViewById(R.id.myLinearLayout)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
adjustMarginsBasedOnTime()
}
private fun adjustMarginsBasedOnTime() {
val calendar = Calendar.getInstance()
val currentHour = calendar.get(Calendar.HOUR_OF_DAY)
val marginStart: Int
val marginEnd: Int
val marginTop: Int
val marginBottom: Int
when (currentHour) {
in 6..11 -> { // Morning (6 AM - 11 AM)
marginStart = resources.getDimensionPixelSize(R.dimen.margin_morning_start)
marginEnd = resources.getDimensionPixelSize(R.dimen.margin_morning_end)
marginTop = resources.getDimensionPixelSize(R.dimen.margin_morning_top)
marginBottom = resources.getDimensionPixelSize(R.dimen.margin_morning_bottom)
}
in 12..17 -> { // Afternoon (12 PM - 5 PM)
marginStart = resources.getDimensionPixelSize(R.dimen.margin_afternoon_start)
marginEnd = resources.getDimensionPixelSize(R.dimen.margin_afternoon_end)
marginTop = resources.getDimensionPixelSize(R.dimen.margin_afternoon_top)
marginBottom = resources.getDimensionPixelSize(R.dimen.margin_afternoon_bottom)
}
in 18..23 -> { // Evening/Night (6 PM - 5 AM)
marginStart = resources.getDimensionPixelSize(R.dimen.margin_evening_start)
marginEnd = resources.getDimensionPixelSize(R.dimen.margin_evening_end)
marginTop = resources.getDimensionPixelSize(R.dimen.margin_evening_top)
marginBottom = resources.getDimensionPixelSize(R.dimen.margin_evening_bottom)
}
else -> { // Should not happen, but a default case
marginStart = resources.getDimensionPixelSize(R.dimen.default_margin)
marginEnd = resources.getDimensionPixelSize(R.dimen.default_margin)
marginTop = resources.getDimensionPixelSize(R.dimen.default_margin)
marginBottom = resources.getDimensionPixelSize(R.dimen.default_margin)
}
}
linearLayout.updateLayoutParams<LinearLayout.LayoutParams> {
setMargins(marginStart, marginTop, marginEnd, marginBottom)
}
}
}
下面是fragment_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/teal_200">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/layout_text"
android:textColor="@android:color/holo_red_dark"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
您得到的错误是 FrameLayout LayoutParams 无法转换为 LinearLayout LayoutParams。这意味着该错误是由 LinearLayout 的 LayoutParams 被视为 FrameLayout 的 LayoutParams 引起的。这是您需要的修复。
linearLayout.updateLayoutParams<FrameLayout.LayoutParams> {
setMargins(marginStart, marginTop, marginEnd, marginBottom)
}
但是,我强烈建议您将 when 语句范围更新为下面的代码,因为您目前尚未涵盖所有 24 小时(0 到 23)
when (currentHour) {
in 0..11 -> { // Morning (0 AM - 11 AM)
}
in 12..17 -> { // Afternoon (12 PM - 5 PM)
}
in 18..23 -> { // Evening/Night (6 PM - 5 AM)
}
else -> { // Would not happen now!, but a default case
}
}
最后!就像 CommonsWare 所说,你应该在你的问题中添加错误日志,这样你就可以帮助别人帮助你。
干杯,快乐编码。