【问题标题】:Issue with programmatically trying to set a FrameLayout for Android以编程方式尝试为 Android 设置 FrameLayout 的问题
【发布时间】:2021-07-31 21:00:35
【问题描述】:

我可以通过如下图所示的 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);

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

更新:

根据屏幕截图下方的响应,结果如下:

【问题讨论】:

  • layout_ 开头的XML 属性是View 上的LayoutParams 的设置,而不是View 本身的设置。您需要为TextView 创建一个LayoutParams,以说明layout_width_height_gravity;例如,new FrameLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT, Gravity.Bottom)ImageView 不一定需要任何东西,因为 FrameLayout 包装了它。您还需要修复setGravity() 调用,除非FrameLayout 本身在另一个FrameLayout 中,否则在其上设置FrameLayout.LayoutParams 可能没有意义。
  • 不确定你是如何使用布局得到这个结果的,逻辑上父级是wrap_content,而TextViewmatch_parent;那么这意味着wrap_content 并且您以编程方式获得.. 如果您对FrameLayout以外的任何其他类型的布局感兴趣,请告诉我
  • @Zain 我没有嫁给 framelayout,但据我了解,framelayout 是唯一可以用来将一个元素覆盖在另一个元素上的布局?!我需要图像视图位于图像视图的“背景”中,文本视图位于图像视图的“前景”中,如第一张图像所示。
  • Zain 对我没有想到的TextView 提出了一个很好的观点。不过,您可以使用它的layout_gravity 来纠正它,您可以在代码中将其更改为Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL。 (除非你真的想要背景底部的灰色条纹。现在还不清楚你到底要做什么。)
  • @MikeM。是的,灰色条纹也是需要的。

标签: android android-layout android-framelayout


【解决方案1】:

您需要将布局重力作为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);

【讨论】:

  • 你是一个救生员!!!!!!它完全按照我的需要工作!!!!
【解决方案2】:

对不起,我改成 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);

【讨论】:

  • 我收到以下代码行错误Cannot resolve method 'setGravity' in 'LayoutParams'layoutparams.setGravity(Gravity.BOTTOM);
  • 对不起,我更新了,
  • 还是不行,我已经根据你上面提供的代码更新了截图结果。您能否根据我的上述内容和您在回答中的建议提供完整的代码。
猜你喜欢
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
  • 2014-04-04
相关资源
最近更新 更多