如何更改TextView的边距


82

我已经以编程方式将TextView添加到LinearLayout中,并且在某些外部事件中,我想将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();

修改已添加到视图的小部件边距的正确方法是正确的吗?
有些不起作用。

Answers:


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

我做到了,而且效果很好。也许当您在-ve中提供值时,这就是代码无法正常工作的原因。您只需将这段代码放在创建视图引用的位置即可。


此调用需要API级别19
桑杰库马尔

4
@SanjayKumar-不,没有限制,它要求API级别19
Debarati 2014年

1
这如何回答OP的问题?似乎代码是相同的,除了冗余的额外行设置触摸侦听器,不调用invalidate(),并且没有在构造函数中显式声明LinearLayout.LayoutParams [LayoutParams.WRAP_CONTENT vs LinearLayout.LayoutParams.WRAP_CONTENT]中的参数?另外(不是很重要),OP要求在程序创建的TextView上设置边距,尽管答案是通过findViewById()从布局xml获取TextView的。我很好奇我在这里想念的东西
Melquiades 2015年

@Melquiades同意您的观点,将其删除为选定答案,因为它没有回答问题,也正如答案中提到的那样,Maybe as you are giving the value in -ve可以是-ve值。
2015年

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

这是一个棘手的问题,我在表格布局的一行中将margin设置为textview。见下面:

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;

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

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

导入android.widget。LinearLayout .LayoutParams;

因此请相应地使用。


5

setMargins()设置TextView的内部页边距,而不是布局边距。那是你想做的吗?这两个不同的边距可能非常复杂。

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

您无需在LinearLayout上进行任何更改。

问候,奥利弗


3

TextView不支持setMargins。Android文档说:

即使视图可以定义填充,它也不能为边距提供任何支持。但是,视图组提供了这种支持。有关更多信息,请参考ViewGroup和ViewGroup.MarginLayoutParams。


1

这是另一种方法...

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

  1. 可见性设置为“消失”(不占用任何空间。)
  2. 高度设置为我需要的分隔距离。

    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

绘制完之后,您可能正在更改布局边距。mOldTextView.invalidate()是无用的。您需要在父级上调用requestLayout()来重新分配新配置。当您在进行绘图之前移动布局更改代码时,一切工作正常。


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中textviewParent强制转换为相应的Params 。

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

要在不同设备上渲染相同的实际大小,请使用我上面使用的DptoPxConvertion()方法。setMargin(left,top,right,bottom)参数将以像素为单位而不是dp中的值。有关更多参考,请参见此链接答案

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.