admin管理员组文章数量:1126305
I am trying to change the margin of a linear layout in Kotlin programmatically as shown below. But the app crashes as soon I click on the page. There is no error during build. I searched here and couldn't find a working solution. Hope to get an answer here. Thanks!
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)
}
}
}
Below is the fragment_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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>
I am trying to change the margin of a linear layout in Kotlin programmatically as shown below. But the app crashes as soon I click on the page. There is no error during build. I searched here and couldn't find a working solution. Hope to get an answer here. Thanks!
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)
}
}
}
Below is the 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>
Share
Improve this question
asked Jan 8 at 23:07
TopaZTopaZ
455 bronze badges
1
- 2 "But the app crashes as soon I click on the page" -- use Logcat and examine the stack trace associated with your crash. If you do not understand the stack trace, please edit your question and post it. – CommonsWare Commented Jan 8 at 23:11
1 Answer
Reset to default 0The error you got is that FrameLayout LayoutParams can't be cast to LinearLayout LayoutParams. This means that the error is caused by the LayoutParams of your LinearLayout being seen as that of a FrameLayout. Here is the fix you need.
linearLayout.updateLayoutParams<FrameLayout.LayoutParams> {
setMargins(marginStart, marginTop, marginEnd, marginBottom)
}
However, I strongly suggest you update your when statement range to the code below as you are currently not covering all 24 hours (0 to 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
}
}
Lastly! Like CommonsWare said, you're supposed to add the error log in your question so you can help others help you.
Cheers and Happy Coding.
版权声明:本文标题:Change Linear Layout Margin in a fragment at different time of the day in Android Kotlin - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736683255a1947531.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论