如果我使用xml文件,我有一个名为layout_margin的选项(例如layout_margin =“1dp”用于文本视图),但我想以编程方式设置,我不知道该怎么做。
答案 0 :(得分:14)
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)textview.getLayoutParams();
params.setMargins(20, 0, 0, 0);
textview.setLayoutParams(params);
答案 1 :(得分: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);
答案 2 :(得分: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);
答案 3 :(得分: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);
答案 4 :(得分:2)
注意,并非所有LayoutParams都有方法setMargins();
RelativeLayout,LinearLayout等有自己的内部类LayoutParams,因此setMargins的可用性并不总是可用。
答案 5 :(得分:1)
试试这个:它有用......
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
creationComplete="application1_creationCompleteHandler(event)">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
[Bindable] public var isDoctor:Boolean;
[Bindable] public var isLawyer:Boolean;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
isDoctor = Session.isDoctor();
isLawyer = Session.isLawyer();
}
]]>
</fx:Script>
<s:VGroup>
<s:Label text="Some Text Lawyer" includeInLayout="{isLawyer}" visible="{isLawyer}"/>
<s:Label text="Some Text Doctor" includeInLayout="{isDoctor}" visible="{isDoctor}"/>
</s:VGroup>
</s:Application>
答案 6 :(得分:0)
您可以使用setMargins()
上的LinearLayout.LayoutParams
。有关详细信息,请参阅this StackOverflow question的答案。
答案 7 :(得分: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如上所述。如果parent是LinearLayout则意味着
LinearLayout.LayoutParams mLinearlp = (LinearLayout.LayoutParams) tv
.getLayoutParams();
要在不同设备上呈现相同的实际大小,请使用我上面使用的DptoPxConvertion()方法。 setMargin(left,top,right,bottom)params将以像素为单位值而不是dp 。