How to Change Margin of TextView

前端 未结 8 1832
感动是毒
感动是毒 2020-12-04 13:11

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. <

相关标签:
8条回答
  • 2020-12-04 13:58

    You were probably changing the layout margin after it has been drawn. mOldTextView.invalidate() is useless. you needed to call requestLayout() on the parent to relayout the new configuration. When you moved the layout changing code before the drawing took place, everything worked fine.

    0 讨论(0)
  • 2020-12-04 13:59

    Here is another approach...

    When I've got to the same problem, I didn't like the suggested solutions here. So, I've come up with another way: I've inserted a TextView in the XML file between the two fields I wanted to separate with two important fields:

    1. visibility set to "GONE" (doesn't occupy any space..)
    2. height is set to whatever I needed the separation to be.

      XML:
      ...//some view up here
      <TextView
          android:id="@+id/dialogSeparator"
          android:layout_width="match_parent"
          android:layout_height="30dp"
          android:visibility="gone"/>
      ...//some view down here
      

    Now, I the code, all I needed to do it simple change the visibility to invisible (i.e. it's there, and taking the needed space, but it's unseen)

        JAVA:
    
        TextView tvSeparator = (TextView)activity.findViewById(R.id.dialogSeparator);
        tvSeparator.setVisibility(View.INVISIBLE);
        //Inside an activity extended class I can use 'this' instead of 'activity'.
    

    Viola...I got the needed margin. BTW, This solution is for LinearLayout with vertical orientation, but you can do it with different layouts.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题