2011-01-27 62 views
76

我将TextView添加到了LinearLayout中,并且在一些外部事件中,我想将该TextView的底部边距减小到-10,为此我尝试了下面的内容。如何更改TextView的页边距

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

修改已添加到视图的小部件的边距是否正确?
一些它如何不工作。

回答

5

setMargins()设置TextView的INNER边距,而不是布局边距。那是你想要做的吗?这两个不同的利润可能相当复杂。

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

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

问候, 奥利弗

174
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中给出了值,这就是为什么你的代码无法工作。 您只需将此代码放置在您创建视图引用的位置。

+0

固体。谢谢... – trgraglia

+0

这个调用需要API级别为19. –

+4

@SanjayKumar - 不,没有限制,它需要API级别19 – Debarati

1

TextView不支持setMargins。 Android docs说:

即使视图可以定义填充,它不提供任何支持边距。但是,查看组提供了这样的支持。有关更多信息,请参阅ViewGroup和ViewGroup.MarginLayoutParams。

58

你在XML的布局可能已经有一个layout_margin在它(左| |右等)的属性,这意味着你需要访问由XML生成的对象,并对其进行修改。

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

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

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

break; 

让您的TextView的实例的LayoutParams,其向下转换到MarginLayoutParams,并使用setMargins方法来设置页边距。

+0

这给了我ClassCastException: - > ViewGroup.LayoutParams不能转换为ViewGroup.MarginLayoutParams –

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; 

import android.widget。 LinearLayout .LayoutParams;

所以相应地使用。

0

您可能在绘制后更改布局边距。 mOldTextView.invalidate()没用。您需要调用父级的requestLayout()来重新布局新配置。在绘图发生之前移动布局更改代码时,一切正常。

1

这里是另一种方法...

当我有同样的问题,我不喜欢这里建议的解决方案。 所以,我想出了另一种方式: 我所插入的两个领域,我想有两个重要的领域分离的XML文件中的一个TextView:

  1. 可见性设置为“GONE”(没有按't占据任何空间..)
  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,但您可以使用不同的布局。

希望这会有所帮助。的TextView的

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

getLayoutParams()应基于在XML的父TextView的的被铸造成相应PARAMS。

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

为了使在不同的设备相同的实际尺寸用我所上面使用DptoPxConvertion()方法。 setMargin(左,上,右,下)params将取像素值,而不是dp。进一步参考请看Link Answer