2012-08-30 250 views
1

我想在android中以編程方式編碼下面的代碼行。 請任何人幫我解決它。我有搜索它,但無法得到確切的解決方案。
Thanx提前。Textview的動態SetRightMagin

android:layout_marginRight="30dp" 

回答

3
Try this, you can do that in code like following 

TextView forgot_pswrd = (TextView) findViewById(R.id.ForgotPasswordText); 
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
llp.setMargins(0, 0, 30, 0); // llp.setMargins(left, top, right, bottom); 
forgot_pswrd.setLayoutParams(llp); 

注:上面的代碼是有效的,如果你正在使用的LinearLayout,如果您使用的RelativeLayout或FrameLayout裏,然後相應地使用。對於這個附加說明,信貸轉到biovamp。

+1

這是正確的,但要確保你的佈局是'LinearLayout'。在其他情況下,使用'FrameLayout','RelativeLayout'等。 –

+0

是的你是對的。 +爲您提供支持。 – VendettaDroid

1

你可以這樣做

TextView tv; 

tv=(TextView)findViewById(R.id.YourTextViewID); 

tv..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); 
+0

請學習如何在這裏正確格式化代碼。還要用完整的英文句子拼出單詞,而不是用你和你的單詞。 –