开发者

Place TextView created in code, under TextView in XML

开发者 https://www.devze.com 2023-02-23 08:35 出处:网络
I have searched high and low but can\'t get this to work. TextView topic = (TextView) findViewById(R.id.topic);

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.getResour开发者_Python百科ces().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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消