我正在尝试通过代码来修改EditText的marginTop……我读到了关于setLayoutParams的内容,但我得到了ForceClose和这条消息:
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" />发布于 2011-08-05 20:07:20
试试这个:
LayoutParams params = EditTextName.getLayoutParams();
params.setMargins(left, top, right, bottom);
EditTextName.setLayoutParams(params);发布于 2011-08-05 20:06:08
类类型转换异常我通常会发现我的R文件是过时的,尝试清理您的项目并重新构建
发布于 2011-08-05 20:20:01
您必须从EditText中获取LayoutParams,而不是创建新的LayoutParams。您可以尝试这样做:
EditText giveuser = (EditText) findViewById(R.id.txt_username);
LayoutParams lp = giveuser.getLayoutParams();
lp.setMargins(left, top, right, bottom);
giveuser.setLayoutParams(lp);https://stackoverflow.com/questions/6955884
复制相似问题