以编程方式创建的视图无法正常应用规则

时间:2018-04-04 11:49:29

标签: android android-layout

我正在尝试使用两个图像创建自定义对话框类。

这是我的班级:

class TransparentProgressDialog(context: Context) : Dialog(context, R.style.DialogTheme) {
    private val iv_logo: ImageView
    private val iv: ImageView

    init {
        val wlmp = getWindow().getAttributes()

        wlmp.gravity = Gravity.CENTER_HORIZONTAL
        getWindow().setAttributes(wlmp)
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setTitle(null)
        setCancelable(false)
        setOnCancelListener(null)
        val rl = RelativeLayout(this.context)
        val params = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
        params.height = 200
        params.width = 240

        iv = ImageView(context)
        iv.setId(View.generateViewId());
        iv_logo = ImageView(context)
        iv_logo.setId(View.generateViewId());

        Glide.with(context).load(R.drawable.app_logo_ic).into(iv_logo);
        Glide.with(context).asGif().load(R.raw.loading_gif).into(iv);

        rl.addView(iv_logo, params)
        params.addRule(RelativeLayout.BELOW, iv_logo.getId());

        rl.layoutParams = params
        rl.addView(iv, params)
        addContentView(rl, params)
    }

    override fun show() {
        super.show()
    }
}

问题是添加的视图没有显示在彼此之下,而是像这样出现在彼此之上:

enter image description here

这是样式文件(DialogTheme)中的样式配置:

<style name="DialogTheme" parent="android:Theme.Holo.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <!--<item name="android:textColorPrimary">#FFFFFF</item>-->
    <item name="android:backgroundDimEnabled">true</item>
    <!--<item name="android:textColor">#FFFFFF</item>-->
    <!--<item name="android:textStyle">normal</item>-->
    <!--<item name="android:textSize">12sp</item>-->
</style>

奖金问题:我可以使用XML资源实现相同的目标吗?

0 个答案:

没有答案