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
Add a comment  | 

1 Answer 1

Reset to default 0

The 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 KotlinStack Overflow