简体   繁体   English

如何更改TextView的边距

[英]How to Change Margin of TextView

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. 我已将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();

Is the right way of modifying Margin of widget that has been added to View? 修改已添加到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. 也许当你在-ve中给出价值时,这就是你的代码无效的原因。 You just put this code where you are creating the reference of the view. 您只需将此代码放在创建视图引用的位置即可。

Your layout in xml probably already has a layout_margin(Left|Right|etc) attribute in it, which means you need to access the object generated by that xml and modify it. 您在xml中的布局可能已经包含了layout_margin(Left | Right | etc)属性,这意味着您需要访问该xml生成的对象并对其进行修改。

I found this solution to be very simple: 我发现这个解决方案非常简单:

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

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

break;

Get the LayoutParams instance of your textview, downcast it to MarginLayoutParams, and use the setMargins method to set the margins. 获取textview的LayoutParams实例,将其向下转换为MarginLayoutParams,并使用setMargins方法设置边距。

This one is tricky problem, i set margin to textview in a row of a table layout. 这个是棘手的问题,我在表格布局的一行中设置了textview到textview。 see the below: 见下文:

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

the class needed to import for LayoutParams for use in a table row is : 导入LayoutParams以在表格行中使用的类是:

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

important to note that i added the class for table row. 重要的是要注意我添加了表行的类。 similarly many other classes are available to use LayoutParams like: 类似地,许多其他类可用于使用LayoutParams,例如:

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

import android.widget. 导入android.widget。 LinearLayout .LayoutParams; LinearLayout .LayoutParams;

so use accordingly. 所以要相应地使用

setMargins() sets the INNER margins of the TextView, not the layout-margins. setMargins()设置TextView的INNER边距,而不是布局边距。 Is that what you want to do? 那是你想做的吗? This two different margins can be quite complicated. 这两个不同的边距可能非常复杂。

If you want to set the layout margins, change the LayoutParams of the TextView (textview.getLayoutParams(), then change the parameters on the returned LayoutParams object). 如果要设置布局边距,请更改TextView的LayoutParams(textview.getLayoutParams(),然后更改返回的LayoutParams对象上的参数)。

You don't need to change anything on your LinearLayout. 您无需更改LinearLayout上的任何内容。

Regards, Oliver 此致,奥利弗

TextView does not support setMargins. TextView不支持setMargins。 Android docs say: Android文档说:

Even though a view can define a padding, it does not provide any support for margins. 即使视图可以定义填充,它也不提供对边距的任何支持。 However, view groups provide such a support. 但是,视图组提供了这样的支持。 Refer to ViewGroup and ViewGroup.MarginLayoutParams for further information. 有关详细信息,请参阅ViewGroup和ViewGroup.MarginLayoutParams。

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: 所以,我想出了另一种方式:我在XML文件中插入了一个TextView,它在两个我希望用两个重要字段分隔的字段之间:

  1. visibility set to "GONE" (doesn't occupy any space..) 可见性设置为“GONE”(不占用任何空间..)
  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 (ie 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. Viola ...我得到了所需的保证金。 BTW, This solution is for LinearLayout with vertical orientation, but you can do it with different layouts. BTW,此解决方案适用于具有垂直方向的LinearLayout,但您可以使用不同的布局。

Hope this helps. 希望这可以帮助。

You were probably changing the layout margin after it has been drawn. 您可能在绘制后更改了布局边距。 mOldTextView.invalidate() is useless. mOldTextView.invalidate()没用。 you needed to call requestLayout() on the parent to relayout the new configuration. 您需要在父级上调用requestLayout()以重新布局新配置。 When you moved the layout changing code before the drawing took place, everything worked fine. 当您在绘图发生之前移动布局更改代码时,一切正常。

        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() of textview should be casted to the corresponding Params based on the Parent of the textview in xml . textview的getLayoutParams()应该基于xml中textviewParent转换为相应的Params。

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

To render the same real size on different devices use DptoPxConvertion() method which I have used above. 要在不同设备上呈现相同的实际大小,请使用我上面使用的DptoPxConvertion()方法。 setMargin(left,top,right,bottom) params will take values in pixel not in dp . setMargin(left,top,right,bottom)params将获取像素中的值而不是dp中的值 For further reference see this Link Answer 有关详细信息,请参阅此链接答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM