4

私が作成した小さなキーボードを介してテキストを入力する、動的に作成された 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)
4

1 に答える 1

1
import android.widget.LinearLayout.LayoutParams;

パラメータを に追加しますWRAP_CONTENT。また、必要な高さのレイアウト、塗りつぶし、またはラップも設定します。1 つ目は幅、2 つ目は高さです。

 EditText et = new EditText(myContext);

 LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

 et.setLayoutParams(params);
于 2012-08-13T10:45:45.007 に答える