我对动态创建的EditText有一点问题,我通过我创建的小键盘输入文本,取代原来的Android。文本是使用append插入的,但EditText的大小不适合文本。为什么呢?
使用此解决方案:
EditText et = new EditText(myContext);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
et.setLayoutParams(params);
我有这个错误:
E/AndroidRuntime(9544): FATAL EXCEPTION: main
E/AndroidRuntime(9544): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.TableRow$LayoutParams
E/AndroidRuntime(9544): at android.widget.TableRow.mapIndexAndColumns(TableRow.java:162)
E/AndroidRuntime(9544): at android.widget.TableRow.getVirtualChildCount(TableRow.java:147)
E/AndroidRuntime(9544): at android.widget.TableRow.getColumnsWidths(TableRow.java:287)
E/AndroidRuntime(9544): at android.widget.TableLayout.findLargestCells(TableLayout.java:500)
E/AndroidRuntime(9544): at android.widget.TableLayout.measureVertical(TableLayout.java:465)
E/AndroidRuntime(9544): at android.widget.TableLayout.onMeasure(TableLayout.java:431)
E/AndroidRuntime(9544): at android.view.View.measure(View.java:12723)
E/AndroidRuntime(9544): at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1163)
E/AndroidRuntime(9544): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
E/AndroidRuntime(9544): at android.widget.ScrollView.onMeasure(ScrollView.java:312)
E/AndroidRuntime(9544): at android.view.View.measure(View.java:12723)
E/AndroidRuntime(9544): at android.widget.HorizontalScrollView.measureChildWithMargins(HorizontalScrollView.java:1159)
E/AndroidRuntime(9544): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
E/AndroidRuntime(9544): at android.widget.HorizontalScrollView.onMeasure(HorizontalScrollView.java:303)
E/AndroidRuntime(9544): at android.view.View.measure(View.java:12723)
E/AndroidRuntime(9544): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
E/AndroidRuntime(9544): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
E/AndroidRuntime(9544): at android.view.View.measure(View.java:12723)
E/AndroidRuntime(9544): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
E/AndroidRuntime(9544): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
E/AndroidRuntime(9544): at android.view.View.measure(View.java:12723)
E/AndroidRuntime(9544): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
E/AndroidRuntime(9544): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
E/AndroidRuntime(9544): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
E/AndroidRuntime(9544): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
E/AndroidRuntime(9544): at android.view.View.measure(View.java:12723)
E/AndroidRuntime(9544): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
E/AndroidRuntime(9544): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
E/AndroidRuntime(9544): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
E/AndroidRuntime(9544): at android.view.View.measure(View.java:12723)
E/AndroidRuntime(9544): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
E/AndroidRuntime(9544): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
E/AndroidRuntime(9544): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(9544): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(9544): at android.app.ActivityThread.main(ActivityThread.java:4624)
E/AndroidRuntime(9544): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(9544): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(9544): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
E/AndroidRuntime(9544): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
E/AndroidRuntime(9544): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
import android.widget.LinearLayout.LayoutParams;
将参数添加到WRAP_CONTENT
。并设置所需的高度布局,填充或包裹。第一个是宽度,第二个是高度。
EditText et = new EditText(myContext);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
et.setLayoutParams(params);