如何更改TextView的保证金保证金、如何更改、TextView

2023-09-12 11:14:21 作者:谁是你的软肋

我有TextView的编程方式添加到的LinearLayout和一些外部事件,我想,以减少该TextView的底缘至-10,为我尝试以下。

  LinearLayout.LayoutParams lastTxtParams =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
lastTxtParams.setMargins(0,0,0,-10);
mOldTextView.setLayoutParams(lastTxtParams);
mOldTextView.invalidate();
 

时的修改已添加到视图部件的保证金的正确方法? 一些它是如何不工作。

解决方案

  TextView的forgot_pswrd =(TextView中)findViewById(R.id.ForgotPasswordText);
    forgot_pswrd.setOnTouchListener(本);
    LinearLayout.LayoutParams LLP =新LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    llp.setMargins(50,0,0,0); // llp.setMargins(左,上,右,下);
    forgot_pswrd.setLayoutParams(LLP);
 

我这样做,它完美地工作。 也许因为你给的值-ve,这就是为什么你的code不工作。 你只要把你在哪里创建视图的引用这个code。

android studio怎么使imageview

I have TextView added Programmatically in to LinearLayout and on some external events I want to decrease bottom margin of that TextView to -10, for that I tried following.

LinearLayout.LayoutParams lastTxtParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lastTxtParams.setMargins(0, 0, 0, -10);
mOldTextView.setLayoutParams(lastTxtParams);
mOldTextView.invalidate();

Is the right way of modifying Margin of widget that has been added to View? Some how it is not working.

解决方案

    TextView forgot_pswrd = (TextView) findViewById(R.id.ForgotPasswordText);
    forgot_pswrd.setOnTouchListener(this);     
    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    llp.setMargins(50, 0, 0, 0); // llp.setMargins(left, top, right, bottom);
    forgot_pswrd.setLayoutParams(llp);

I did this and it worked perfectly. Maybe as you are giving the value in -ve, that's why your code is not working. You just put this code where you are creating the reference of the view.