我正在尝试将 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)