android - setting LayoutParams programmatically

前端 未结 5 999
南笙
南笙 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条回答
  • 2020-11-29 03:47

    For Xamarin Android align to the left of an object

    int dp24 = (int)TypedValue.ApplyDimension( ComplexUnitType.Dip, 24, Resources.System.DisplayMetrics );
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( dp24, dp24 );
                lp.AddRule( LayoutRules.CenterInParent, 1 );
                lp.AddRule( LayoutRules.LeftOf, //Id of the field Eg m_Button.Id ); 
                m_Button.LayoutParameters = lp;
    
    0 讨论(0)
  • 2020-11-29 03:48
    int dp1 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1,
                context.getResources().getDisplayMetrics());
    
    tv.setLayoutParams(new ViewGroup.LayoutParams(
    ViewGroup.LayoutParams.WRAP_CONTENT,
    dp1 * 100)); // if you want to set layout height to 100dp
    
    llview.addView(tv);
    
    0 讨论(0)
  • 2020-11-29 03:49

    Just replace from bottom and add this

    tv.setLayoutParams(new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT));
    

    before

    llview.addView(tv);
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-11-29 04:00
      LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                       /*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
                       /*height*/ ViewGroup.LayoutParams.MATCH_PARENT,
                       /*weight*/ 1.0f
                );
                YOUR_VIEW.setLayoutParams(param);
    
    0 讨论(0)
提交回复
热议问题