2012-08-13 126 views
4

我有一個動態創建的EditText的小問題,我通過我創建的小鍵盤輸入文本,取代原來的Android。文本是插入的附加,但EditText的大小不適合文本。爲什麼?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

是的,我創建了一個小鍵盤和禁用經典的Android – whiteTIGER 2012-08-13 10:17:56

+0

使用WRAP_CONTENT – Doomsknight 2012-08-13 10:23:00

+0

我要自動調整由內容的EditText上,我不得不這樣做動態java中 – whiteTIGER 2012-08-13 10:30:07

回答

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); 
+0

您的解決方案對我無效,因爲我的EditText動態創建,插入到TableRow,Horizo​​ntalScrollView,Scrollview,TableLayout和FrameLyaout的系統中。我需要一些直接影響EditText的措施 – whiteTIGER 2012-08-13 11:07:13

+0

這是設置這種佈局的編程方式。與使用XML相反。當你動態創建你的EditText時,應用這個。 – Doomsknight 2012-08-13 11:17:57

+0

我編輯我的問題與logcat – whiteTIGER 2012-08-13 11:26:49