如何更改TextView的边距

84

我在LinearLayout中通过编程方式添加了一个TextView,并希望在某些外部事件中将该TextView的底部边距减小到 -10。为此,我尝试了以下方法。

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

修改已添加到视图中的小部件的边距的正确方法是什么?
不知何故,它不起作用。


请查看KOTLIN的这个链接:https://dev59.com/dGUo5IYBdhLWcg3w9jV4#72072222 - Mori
8个回答

189
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);
我这样做了,它完美地运行了。 可能是因为您正在使用负数赋值,这就是为什么您的代码无法正常工作的原因。 将此代码放在您正在创建视图引用的位置即可。

此调用需要API级别19。 - Sanjay Kumar
4
@SanjayKumar - 不,没有要求需要API 19级的限制。 - Debarati
1
这个回答如何解决OP的问题?代码似乎是一样的,除了多余的设置触摸监听器、不调用invalidate()和在LinearLayout.LayoutParams构造函数中没有明确指定参数[LayoutParams.WRAP_CONTENT vs LinearLayout.LayoutParams.WRAP_CONTENT]。此外(虽然可能无关紧要),OP正在询问如何在通过findViewById()从布局xml获取TextView的情况下设置编程创建的TextView的边距。我很好奇我错过了什么... - Melquiades
@Melquiades 同意你的观点,我将选定的答案移除了,因为它并没有回答问题。同时,正如答案中提到的那样,“也许你给出的值是负数”,边距可以是负值。 - User7723337

61

你的XML布局中可能已经有了一个layout_margin(Left|Right|etc)属性,这意味着你需要访问由该XML生成的对象并对其进行修改。

我发现这个解决方案非常简单:

ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) mTextView
        .getLayoutParams();

mlp.setMargins(adjustmentPxs, 0, 0, 0);

break;

获取你的TextView的LayoutParams实例,将其向下转型为MarginLayoutParams,并使用setMargins方法设置边距。


这个错误提示是ClassCastException:-> ViewGroup.LayoutParams无法转换为ViewGroup.MarginLayoutParams。 - Harshil Pansare

10

这是一个棘手的问题,我在表格布局中的一行中为TextView设置了margin。

看下面的代码:

TableLayout tl = new TableLayout(this);
tl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

TableRow tr = new TableRow(this);        
tr.setBackgroundResource(R.color.rowColor);

LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(4, 4, 4, 4);

TextView tv = new TextView(this);
tv.setBackgroundResource(R.color.textviewColor);
tv.setText("hello");
tr.addView(tv, params);

TextView tv2 = new TextView(this);
tv2.setBackgroundResource(R.color.textviewColor);
tv2.setText("hi");
tr.addView(tv2, params);

tl.addView(tr);
setContentView(tl);

在表格行中使用LayoutParams所需导入的类是:

import android.widget.**TableRow**.LayoutParams;

需要注意的是我为表格行添加了class。类似地,还有许多其他可用于使用LayoutParams的类,如:

import android.widget.**RelativeLayout**.LayoutParams;

导入 android.widget.LinearLayout.LayoutParams;

之后根据需要使用即可。


6

setMargins()方法设置的是TextView内部的边距,而不是布局边距。您想要做的是这个吗?这两种不同的边距可能会相当复杂。

如果您想要设置布局边距,请更改TextView的LayoutParams(textview.getLayoutParams()),然后更改返回的LayoutParams对象上的参数。

您不需要更改LinearLayout上的任何内容。

祝好, Oliver


3
TextView不支持setMargins方法。根据Android文档的说明:
尽管视图可以定义padding,但它不提供任何对margins的支持。然而,view groups提供了这样的支持。有关详细信息,请参阅ViewGroup和ViewGroup.MarginLayoutParams。

1

这里有另一种方法...

当我遇到同样的问题时,我不喜欢这里提出的解决方案。因此,我想出了另一种方法:在XML文件中插入一个TextView,将我想要分隔的两个字段与两个重要字段分隔开。

  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
    
现在,我需要做的只是简单地将可见性更改为不可见(即,它存在并占用所需的空间,但是它是看不到的)。
    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'.

弗洛拉...我得到了所需的边距。

顺便提一下,此解决方案适用于垂直方向的LinearLayout,但您也可以使用不同的布局。

希望这能有所帮助。


0
        TextView tv = (TextView)findViewById(R.id.item_title));
        RelativeLayout.LayoutParams mRelativelp = (RelativeLayout.LayoutParams) tv
                    .getLayoutParams();
        mRelativelp.setMargins(DptoPxConvertion(15), 0, DptoPxConvertion (15), 0);
        tv.setLayoutParams(mRelativelp);

    private int DptoPxConvertion(int dpValue)
    {
       return (int)((dpValue * mContext.getResources().getDisplayMetrics().density) + 0.5);
    }

在编程中,应该将TextView的getLayoutParams()强制转换为基于XML中TextView父级对应的Params。

<RelativeLayout>
   <TextView
    android:id="@+id/item_title">
</RelativeLayout>

为了在不同的设备上呈现相同的实际大小,请使用我上面使用过的DptoPxConvertion()方法。setMargin(left,top,right,bottom)参数将采用像素值而非dp值。如需进一步参考,请查看此链接答案


0
你可能是在已经绘制后更改了布局边距。mOldTextView.invalidate() 是无用的。你需要调用父级的 requestLayout() 方法来重新布置新的配置。当你将布局更改代码移动到绘图之前,一切都正常工作了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接