Fragment overlaps the AppCompat toolbar

后端 未结 5 1001
感动是毒
感动是毒 2020-12-13 09:41

I\'m working with the v7 support library and trying to have a navigation drawer on the left. As read elsewhere I set up:

  1. DrawerTest.java: T

5条回答
  •  萌比男神i
    2020-12-13 10:09

    I have two activities and attach one fragment to them. In first activity it is shown right, while in second it overlaps Toolbar. Though android:layout_marginTop="?android:attr/actionBarSize" in a layout of the fragment may be a solution, I found an error.

    Because Kotlin caches ids of views of layouts (instead of findViewById(R.id.some_id) you can write some_id), there is a big problem. Every time you should be very careful when copy an activity or a fragment. So, every time check your imports and attached layouts.

    I copied FirstActivity to SecondActivity, copied activity_first to activity_second, but forgot to change:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_first) // Don't forget to change.
    

    Similarly, in a fragment don't forget:

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
    
        val view = inflater.inflate(R.layout.fragment_yours, container, false)
    

    So, in my imports I had:

    import kotlinx.android.synthetic.main.activity_second.*
    

    In this case toolbar referenced to activity_second, while activity_first was actually attached. Then the fragment moved upper - just below the Toolbar. In most cases Kotlin never shows any problem with cached ids during compile or run time. It's a big pain.

提交回复
热议问题