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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11504635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:51:44  来源:igfitidea点击:

layout margin for text view programmatically

android

提问by sivan esan

If I use xml file I have a option called layout_margin (for example layout_margin ="1dp" for text view ) but I want to set programmatically and I dont know how to do it.

如果我使用 xml 文件,我有一个名为 layout_margin 的选项(例如 layout_margin ="1dp" for text view )但我想以编程方式进行设置,但我不知道该怎么做。

回答by Andro Selva

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

回答by MicroEyes

Please Google before adding your question to StackOverflow.

在将您的问题添加到 StackOverflow 之前,请谷歌。

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

回答by AkashG

You can do by this:

你可以这样做:

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

回答by ViliusK

Note, that not all LayoutParams has method setMargins();

请注意,并非所有 LayoutParams 都有方法 setMargins();

RelativeLayout, LinearLayout etc has their own inner class LayoutParams, so availability of setMargins is not always available.

RelativeLayout、LinearLayout 等有它们自己的内部类 LayoutParams,所以 setMargins 的可用性并不总是可用的。

回答by Nermeen

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

回答by Ankita

Try This : It worked ....

试试这个:它有效......

LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);

Params.SetMargins(0, 10, 0, 0);

FirstName.LayoutParameters = Params;

回答by anand krish

        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>

If parent of the TextView is RelativeLayout means, then RelativeLayout.LayoutParams as above. If parent is LinearLayout means then

如果TextView 的parent 是RelativeLayout 的意思,那么RelativeLayout.LayoutParams 如上。如果 parent 是 LinearLayout 意味着

LinearLayout.LayoutParams mLinearlp = (LinearLayout.LayoutParams) tv
                    .getLayoutParams();

To render the same real size on different devices use DptoPxConvertion() method which I have used above. setMargin(left,top,right,bottom) params will take values in pixel not in dp.

要在不同的设备上渲染相同的实际大小,请使用我上面使用的 DptoPxConvertion() 方法。setMargin(left,top,right,bottom) 参数将采用像素而不是 dp 的值

回答by Angelo

You can use the setMargins()on the LinearLayout.LayoutParams. See the answer of this StackOverflow questionfor more information.

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