android - setting LayoutParams programmatically

前端 未结 5 1037
南笙
南笙 2020-11-29 03:00

I putting an in-game chat module into an app. I am adding text messages as they are received into a LinearLayout view. I want to set the layout params to the TextView but th

5条回答
  •  Happy的楠姐
    2020-11-29 03:56

    after creating the view we have to add layout parameters .

    change like this

    TextView tv = new TextView(this);
    tv.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    
    llview.addView(tv);
    tv.setTextColor(Color.WHITE);
    tv.setTextSize(2,25);
    tv.setText(chat);
    if (mine) {
        leftMargin = 5;
        tv.setBackgroundColor(0x7C5B77);
    }
    else {
        leftMargin = 50;
        tv.setBackgroundColor(0x778F6E);
    }
    final ViewGroup.MarginLayoutParams lpt =(MarginLayoutParams)tv.getLayoutParams();
    lpt.setMargins(leftMargin,lpt.topMargin,lpt.rightMargin,lpt.bottomMargin);
    

提交回复
热议问题