0

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

1 回答 1

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或者只是从头开始导入。现在,导入是 for 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);
于 2012-12-25T20:46:15.107 回答