0

我正在擴展RelativeLayout來創建片段裝飾器。將ViewGroup作爲片段包裝器擴展

像這樣:

<br.com.simplepass.mapfragmentwrapper.MapFragmentWrapper 
     android:id="@+id/map_wrapper" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <fragment 
      android:id="@+id/main_fragment_map" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      class="com.google.android.gms.maps.MapFragment"/> 

</br.com.simplepass.mapfragmentwrapper.MapFragmentWrapper> 

嗯,我想在RelativeLayout的添加一些圖像,所以它總是在片段的頂部得到,所以我這樣做:

class MapFragmentWrapper : RelativeLayout { 

    var mMarkImageView : ImageView? = null 
    var mShadowView : View? = null 

    constructor(context: Context) : super(context) { 
     init(context) 
    } 

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { 
     init(context) 
    } 

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { 
     init(context) 
    } 

    @TargetApi(23) 
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) { 
     init(context) 
    } 

    private fun init(context: Context) { 

     val params = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
       ViewGroup.LayoutParams.WRAP_CONTENT) 
     params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); 

     mMarkImageView = ImageView(context) 
     mMarkImageView?.setImageResource(R.drawable.ic_marker_centered) 

     mShadowView = View(context) 
     mShadowView?.setBackgroundResource(R.drawable.map_pin_shadow) 

     addView(mMarkImageView, params) 
     addView(mShadowView, params) 


    } 
[some more code here...] 

}

但是,當我的活動開始時,片段會在我添加的視圖之上(mMarkImageView和mShadowView)......這完全與想要的相反。

那麼如何以編程方式將視圖放在視圖組的最後位置?

任何幫助表示讚賞!

回答

0

所以,我找到了答案:

override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { 
     super.onLayout(changed, l, t, r, b) 

     removeView(mMarkImageView) 
     removeView(mShadowView) 
     addView(mMarkImageView, -1 , params) 
     addView(mShadowView, -1, params) 
    } 

這樣做,我把佈局在的ViewGroup結束