在Java中将TextView的宽度更改为wrap_content

时间:2012-12-25 20:43:16

标签: android textview

我正在尝试将TextView的宽度更改为wrap_content

到目前为止我尝试的是:

TextView txt = (TextView) findViewById(R.id.txtViewContents);
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

但是当这个应用程序运行时崩溃了,而这就是我在logcat中得到的结果:

E/AndroidRuntime(2213): FATAL EXCEPTION: main
E/AndroidRuntime(2213): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.support.v7.widget.GridLayout$LayoutParams
E/AndroidRuntime(2213):     at android.support.v7.widget.GridLayout.computeLayoutParamsHashCode(GridLayout.java:872)
E/AndroidRuntime(2213):     at android.support.v7.widget.GridLayout.checkForLayoutParamsModification(GridLayout.java:879)
E/AndroidRuntime(2213):     at android.support.v7.widget.GridLayout.onMeasure(GridLayout.java:927)
E/AndroidRuntime(2213):     at android.view.View.measure(View.java:15513)
E/AndroidRuntime(2213):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
E/AndroidRuntime(2213):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(2213):     at android.view.View.measure(View.java:15513)
E/AndroidRuntime(2213):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
E/AndroidRuntime(2213):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
E/AndroidRuntime(2213):     at android.view.View.measure(View.java:15513)
E/AndroidRuntime(2213):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
E/AndroidRuntime(2213):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(2213):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
E/AndroidRuntime(2213):     at android.view.View.measure(View.java:15513)
E/AndroidRuntime(2213):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
E/AndroidRuntime(2213):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
E/AndroidRuntime(2213):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
E/AndroidRuntime(2213):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
E/AndroidRuntime(2213):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
E/AndroidRuntime(2213):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
E/AndroidRuntime(2213):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
E/AndroidRuntime(2213):     at android.view.Choreographer.doFrame(Choreographer.java:532)
E/AndroidRuntime(2213):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
E/AndroidRuntime(2213):     at android.os.Handler.handleCallback(Handler.java:725)
E/AndroidRuntime(2213):     at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(2213):     at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(2213):     at android.app.ActivityThread.main(ActivityThread.java:5039)
E/AndroidRuntime(2213):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(2213):     at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(2213):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(2213):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(2213):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

尝试改为

setLayoutParams(new android.support.v7.widget.GridLayout.LayoutParams( android.support.v7.widget.GridLayout.LayoutParams.WRAP_CONTENT, android.support.v7.widget.GridLayout.LayoutParams.WRAP_CONTENT));

或者只是从头开始导入android.support.v7.widget.GridLayout.LayoutParams。 现在,导入是ViewGroup.LayoutParams,这不是GridLayout用于布局参数的。

编辑:由于您已经使用ViewGroup.LayoutParams制作了与原先相同的参数,但是将它们分配给变量,让我们称之为viewGroupParams(所以请保持ViewGroup.LayoutParams导入)然后执行此操作:

android.support.v7.widget.GridLayout.LayoutParams newParams = new android.support.v7.widget.GridLayout.LayoutParams (viewGroupParams);

最后

txt.setLayoutParams (newParams);