0

我正在扩展一个 RelativeLayout 来制作一个片段装饰器。

像这样:

<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)之上......这与想要的完全相反。

那么如何以编程方式将视图放在视图组中的最后一个位置?

任何帮助表示赞赏!

4

1 回答 1

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)
    }

这样做我把布局放在视图组的末尾

于 2016-11-23T15:38:29.237 回答