以编程方式尝试为 Android 设置 FrameLayout 时出现问题

Issue with programmatically trying to set a FrameLayout for Android

我能够通过 XML 布局成功实现此目的,如下图所示:

然后在 Java 内尝试这样做时,我无法获得与屏幕截图相同的结果:

这是我的 Java 代码,可以呈现上面的结果:

FrameLayout frameLayout = new FrameLayout(this.context);
FrameLayout.LayoutParams layoutparams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
frameLayout.setLayoutParams(layoutparams);


ImageView bodyImageView = new ImageView(this.context);
bodyImageView.setBackgroundResource(R.drawable.niners);


TextView bodyTextView = new TextView(this.context);
bodyTextView.setBackgroundColor(Color.TRANSPARENT);
bodyTextView.setText("SF");
bodyTextView.setTextColor(Color.YELLOW);
bodyTextView.setGravity(Gravity.BOTTOM);


frameLayout.addView(bodyImageView);
frameLayout.addView(bodyTextView);

谁能看出我遗漏了什么或做错了什么?

更新:

根据屏幕截图下方的响应得出以下结果:

对不起,我改用 kotlin,但是,请测试此代码

       val frameLayout = FrameLayout(this)
        val layoutparams = FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            Gravity.CENTER_VERTICAL or Gravity.CENTER_HORIZONTAL
        )

        frameLayout.layoutParams = layoutparams
        
        val bodyImageView = ImageView(this)
        bodyImageView.setBackgroundResource(R.drawable.ic_launcher_background)

        val params =
            FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        params.gravity = Gravity.BOTTOM

        val bodyTextView = TextView(this)

        bodyTextView.text = "SF"
        bodyTextView.gravity = Gravity.CENTER
        bodyTextView.setTextColor(Color.YELLOW)

        bodyTextView.setBackgroundColor(R.color.purple_700)

        frameLayout.addView(bodyImageView)
        frameLayout.addView(bodyTextView,params)

您只需将参数添加到您的视图

在Java

        // Add this code
           FrameLayout.LayoutParams layoutparams =
                new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutparams.gravity = Gravity.BOTTOM;

        // Add in your code for textView
        frameLayout.addView(bodyTextView,layoutparams);

您需要将布局重力作为 LayoutParams 添加到 TextView:

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;

并将TextView的重心改为中心:

bodyTextView.setGravity(Gravity.CENTER);

这是整个布局:

FrameLayout frameLayout = new FrameLayout(this.context);
FrameLayout.LayoutParams layoutparams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
frameLayout.setLayoutParams(layoutparams);


ImageView bodyImageView = new ImageView(this.context);
bodyImageView.setBackgroundResource(R.drawable.niners);

TextView bodyTextView = new TextView(this.context);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;
bodyTextView.setLayoutParams(params);
bodyTextView.setBackgroundColor(Color.GRAY);
bodyTextView.setText("SF");
bodyTextView.setTextColor(Color.YELLOW);
bodyTextView.setGravity(Gravity.CENTER);
frameLayout.addView(bodyImageView);
frameLayout.addView(bodyTextView);

setContentView(frameLayout);