2 csh 34 csh_34 于 2013.06.24 11:29 提问

使用代码如何修改 marginTop?

我想使用代码给EditText 修改marginTop 。
我设置了 setLayoutParams 但是获得 ForceClose 和下面的信息:

06-20 14:53:59.715: ERROR/AndroidRuntime(913): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
06-20 14:53:59.715: ERROR/AndroidRuntime(913):     at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1291)
06-20 14:53:59.715: ERROR/AndroidRuntime(913):     at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1238)
06-20 14:53:59.715: ERROR/AndroidRuntime(913):     at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:279)
06-20 14:53:59.715: ERROR/AndroidRuntime(913):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:299)
06-20 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" />

1个回答

cj6585256
cj6585256   2013.06.24 12:41

LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT这个错了
应该是ViewGroup.LayoutParams.WRAP_CONTENT

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!