2011-08-05 65 views
2

我試圖找到一種方法來修改marginTop通過代碼一個EditText ......我讀到setLayoutParams但我得到ForceClose這個消息:如何用代碼修改marginTop?

08-05 14:53:59.715: ERROR/AndroidRuntime(913): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 
08-05 14:53:59.715: ERROR/AndroidRuntime(913):  at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1291) 
08-05 14:53:59.715: ERROR/AndroidRuntime(913):  at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1238) 
08-05 14:53:59.715: ERROR/AndroidRuntime(913):  at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:279) 
08-05 14:53:59.715: ERROR/AndroidRuntime(913):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:299) 
08-05 14:53:59.715: ERROR/AndroidRuntime(913):  at android.view.View.measure(View.java:7964) 

誰能解釋我應該怎麼辦?

giveuser = (EditText) findViewById(R.id.txt_username); 
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     lp.setMargins(5,5,5,5); 
     giveuser.setLayoutParams(lp); 

和XML:

<EditText android:id="@+id/txt_username" 
     android:layout_height="wrap_content" android:layout_width="350px" 
     android:layout_centerHorizontal="true" android:layout_below="@+id/loginsubtitle" 
     android:layout_marginTop="180dip" android:singleLine="true" 
     android:hint="Identifiant" /> 
+0

你有一個轉換異常,你可能使用了與錯誤轉換的findById。你能發佈你的代碼嗎? –

回答

6

試試這個:

LayoutParams params = EditTextName.getLayoutParams(); 
params.setMargins(left, top, right, bottom); 
EditTextName.setLayoutParams(params); 
+0

topMargin不存在 – Gabrielle

+0

尋找params.setMargins(arg0,arg1,arg2,arg3); –

+0

你得到lp作爲linearlayoutparam。獲取edittext layoutparam .....這就是爲什麼你沒有得到你的保證金設置.... –

0

造型異常我通常用我的R檔即將過期的發現,嘗試清潔您的項目,重新建設

0

而不是創建一個新的LayoutParams,你必須得到LayoutParams從你的EditText。你可以試試這個:

EditText giveuser = (EditText) findViewById(R.id.txt_username); 
LayoutParams lp = giveuser.getLayoutParams(); 
lp.setMargins(left, top, right, bottom); 
giveuser.setLayoutParams(lp);