将代码中创建的 TextView 放置在 XML 中的 TextView 下

发布于 2024-10-31 07:53:53 字数 936 浏览 0 评论 0原文

我到处搜索,但无法让它发挥作用。

            TextView topic = (TextView) findViewById(R.id.topic);
        topic.setText(json_data.getString("topic"));


        TextView poster = (TextView) findViewById(R.id.poster);
        poster.setText(json_data.getString("name"));

        TextView detail = (TextView) findViewById(R.id.detail);
        detail.setText(json_data.getString("detail"));

        TextView test = new TextView(this);
        test.setText("test string");
        test.setTextColor(this.getResources().getColor(R.color.red));

        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        p.addRule(RelativeLayout.BELOW, R.id.detail);

        addContentView(test, p);

一切正常,除了最后一个文本视图(测试)仅显示在左上角。我想要它在文本视图(详细信息)下。 XML 是基于

苏伊多代码。

任何帮助将不胜感激,因为这是我的应用程序的最后一点。

I have searched high and low but can't get this to work.

            TextView topic = (TextView) findViewById(R.id.topic);
        topic.setText(json_data.getString("topic"));


        TextView poster = (TextView) findViewById(R.id.poster);
        poster.setText(json_data.getString("name"));

        TextView detail = (TextView) findViewById(R.id.detail);
        detail.setText(json_data.getString("detail"));

        TextView test = new TextView(this);
        test.setText("test string");
        test.setTextColor(this.getResources().getColor(R.color.red));

        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        p.addRule(RelativeLayout.BELOW, R.id.detail);

        addContentView(test, p);

Everything works, other than the last textview (test) which will only show in top left. I want it under the textview (detail).
XML is based on

suedo code.

Any help would be truly appreciated as this is the last bit of my app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再可℃爱ぅ一点好了 2024-11-07 07:53:53

尝试使用 test.setLayoutParams(p); 如下:

    RelativeLayout yourLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.newlayout, null, false);

    setContentView(yourLayout);

    TextView topic = (TextView) findViewById(R.id.topic);
    topic.setText(json_data.getString("topic"));


    TextView poster = (TextView) findViewById(R.id.poster);
    poster.setText(json_data.getString("name"));

    TextView detail = (TextView) findViewById(R.id.detail);
    detail.setText(json_data.getString("detail"));

    TextView test = new TextView(this);
    test.setText("test string");

    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);

    p.addRule(RelativeLayout.BELOW, R.id.detail);

    test.setLayoutParams(p);

    yourLayout.addView(test);

    setContentView(yourLayout);

Try using test.setLayoutParams(p); as such:

    RelativeLayout yourLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.newlayout, null, false);

    setContentView(yourLayout);

    TextView topic = (TextView) findViewById(R.id.topic);
    topic.setText(json_data.getString("topic"));


    TextView poster = (TextView) findViewById(R.id.poster);
    poster.setText(json_data.getString("name"));

    TextView detail = (TextView) findViewById(R.id.detail);
    detail.setText(json_data.getString("detail"));

    TextView test = new TextView(this);
    test.setText("test string");

    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);

    p.addRule(RelativeLayout.BELOW, R.id.detail);

    test.setLayoutParams(p);

    yourLayout.addView(test);

    setContentView(yourLayout);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文