以编程方式设置文本视图的布局边距

9
如果我使用XML文件,我有一个名为layout_margin的选项(例如,对于文本视图,layout_margin ="1dp"),但是我想以编程方式设置它,但我不知道该如何做到。

看看这个 - https://dev59.com/-nE95IYBdhLWcg3wDpcG - AkashG
请查看以下链接:https://dev59.com/rG445IYBdhLWcg3wiayV - Hiral Vadodaria
请查看KOTLIN的这个链接:https://dev59.com/dGUo5IYBdhLWcg3w9jV4#72072222 - Mori
8个回答

15
   LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)textview.getLayoutParams();
     params.setMargins(20, 0, 0, 0); 
     textview.setLayoutParams(params);

6

在将您的问题添加到StackOverflow之前,请先使用Google搜索。

TextView tv = (TextView)findViewById(R.id.my_text_view);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tv.getLayoutParams();
params.setMargins(0, 0, 10, 0); //substitute parameters for left, top, right, bottom
tv.setLayoutParams(params);

3
您可以通过以下方式实现此目标:
TextView text = (TextView) findViewById(R.id.text);   
LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);//pass int values for left,top,right,bottom
text.setLayoutParams(params);

我来精确说明一下,代码如下: params.setMargins(text.setMargins(left, top, right, bottom);//传递 left、top、right、bottom 的 int 值 params.setLayoutParams(params); - Bourdier Jonathan
我得到了这个需要API级别19的提示。 - Lion789

3
请注意,并非所有的LayoutParams都有setMargins()方法;RelativeLayout、LinearLayout等都有自己的内部类LayoutParams,所以setMargins的可用性并不总是可用的。

2
TextView tv = (TextView) findViewById(R.id.tvId);   
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(50, 0, 0, 0); // llp.setMargins(left, top, right, bottom);
tv.setLayoutParams(llp);

1
尝试一下:它有效...
LinearLayout.LayoutParams params = new  LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
    );

 params.setMargins(0, 10, 0, 0);

firstName.setLayoutParams(params);

1
我不知道你是否真正测试过它,但截至07.23,它充满错误,并且你没有以大写字母开头命名变量。我不建议你在没有真正测试的情况下回答问题。 - Vendetta8247

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>

如果TextView的父级是RelativeLayout,则使用RelativeLayout.LayoutParams,如上所示。如果父级是LinearLayout,则使用...
LinearLayout.LayoutParams mLinearlp = (LinearLayout.LayoutParams) tv
                    .getLayoutParams();

要在不同设备上呈现相同的实际大小,请使用我上面使用过的DptoPxConvertion()方法。setMargin(left,top,right,bottom)参数将以像素而不是dp为单位进行设置。

0

您可以在LinearLayout.LayoutParams上使用setMargins()。有关更多信息,请参见此StackOverflow问题的答案。


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