setLayoutParams() 方法不起作用

发布于 2024-10-31 09:29:20 字数 5323 浏览 4 评论 0原文

我正在编写一个代码,根据其应用程序提供的屏幕尺寸来更改其尺寸。它通过 View 的 onSizeChanged() 和 onDraw() 方法获取整个宽度和高度(或其相应视图的 w 和 h)。

它在另一个代码中运行良好,但现在每当我尝试输入命令“setLayoutParams()”时它都会显示一条错误消息,您能帮我解决这个问题吗? setLayoutParams() 实际上在代码中的任何地方都不起作用。下面是主要类代码。

public class ManipulateDesign extends Activity {
    private LinearLayout ll;
    private ScrollView sv;
    private TextView tv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ll = (LinearLayout) findViewById(R.id.ll);
        sv = (ScrollView) findViewById(R.id.sv);
        tv = new TextView(this);

        tv.setText("You're in the first ScrollView\n" +
                "- Living Universe \n" +
                "Wish (upon a star) Hope to (be again)\n" +
                "The one you find, when all is done and through!\n" +
                "If all, made a wish to save, the star would it be in vain?\n" +
                "So much it holds, too dear to part, many ways to see the light of answers!\n" +
                "For all was once, just taken away, that one hope all disappeared so none can break nor brittle my heart\n" +
                "no, never again, until the end");

        sv.addView(tv);
        ChangeSizeView csv = new ChangeSizeView(getApplicationContext());
        ll.addView(csv);
    }

    class ChangeSizeView extends View{
        int newWidth;
        int newHeight;

        public ChangeSizeView(Context context) {
            super(context);
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            newWidth = w;
            newHeight = h;
            super.onSizeChanged(w, h, oldw, oldh);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // THIS COMMAND MAKES AN ERROR(not just in this method but anywhere in the entire code) BUT WORKED WELL IN ANOTHER CODE     
            sv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, (newHeight - 100)));         

            super.onDraw(canvas);
        }
    }
}

这是我的 main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/ll"
    >
    <ScrollView android:id="@+id/sv"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content">
    </ScrollView>
</LinearLayout>

这是显示错误消息的 logcat 徽标


04-11 06:39:52.148: ERROR/AndroidRuntime(2952): FATAL EXCEPTION: main
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:355)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.view.View.measure(View.java:8171)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.view.View.measure(View.java:8171)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.view.View.measure(View.java:8171)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.view.View.measure(View.java:8171)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.os.Looper.loop(Looper.java:123)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at java.lang.reflect.Method.invokeNative(Native Method)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at java.lang.reflect.Method.invoke(Method.java:521)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at dalvik.system.NativeStart.main(Native Method)

I am making a code that changes its size according to the screen size its application provides. It gets the entire width and height(or its appropriate view's w and h) by onSizeChanged() and onDraw() methods of View.

It worked well in another code however now it shows an error message whenever I tried to put command 'setLayoutParams()' Would you please help me to figure out this problem? The setLayoutParams() actually does not work anywhere in the code. Below is the main class code.

public class ManipulateDesign extends Activity {
    private LinearLayout ll;
    private ScrollView sv;
    private TextView tv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ll = (LinearLayout) findViewById(R.id.ll);
        sv = (ScrollView) findViewById(R.id.sv);
        tv = new TextView(this);

        tv.setText("You're in the first ScrollView\n" +
                "- Living Universe \n" +
                "Wish (upon a star) Hope to (be again)\n" +
                "The one you find, when all is done and through!\n" +
                "If all, made a wish to save, the star would it be in vain?\n" +
                "So much it holds, too dear to part, many ways to see the light of answers!\n" +
                "For all was once, just taken away, that one hope all disappeared so none can break nor brittle my heart\n" +
                "no, never again, until the end");

        sv.addView(tv);
        ChangeSizeView csv = new ChangeSizeView(getApplicationContext());
        ll.addView(csv);
    }

    class ChangeSizeView extends View{
        int newWidth;
        int newHeight;

        public ChangeSizeView(Context context) {
            super(context);
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            newWidth = w;
            newHeight = h;
            super.onSizeChanged(w, h, oldw, oldh);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // THIS COMMAND MAKES AN ERROR(not just in this method but anywhere in the entire code) BUT WORKED WELL IN ANOTHER CODE     
            sv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, (newHeight - 100)));         

            super.onDraw(canvas);
        }
    }
}

And this is my main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/ll"
    >
    <ScrollView android:id="@+id/sv"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content">
    </ScrollView>
</LinearLayout>

This is the logcat logo that shows error messages


04-11 06:39:52.148: ERROR/AndroidRuntime(2952): FATAL EXCEPTION: main
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:355)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.view.View.measure(View.java:8171)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.view.View.measure(View.java:8171)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.view.View.measure(View.java:8171)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.view.View.measure(View.java:8171)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.os.Looper.loop(Looper.java:123)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at java.lang.reflect.Method.invokeNative(Native Method)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at java.lang.reflect.Method.invoke(Method.java:521)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952):     at dalvik.system.NativeStart.main(Native Method)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毅然前行 2024-11-07 09:29:20

您应该完全限定您需要的 LayoutParams,因为它包含在 LinearLayout 中,您需要使用 new LinearLayout.LayoutParams

You should fully qualify which LayoutParams you need, since it is contained in a LinearLayout, you need to use new LinearLayout.LayoutParams

久夏青 2024-11-07 09:29:20

就我而言,我在 xml 中使用了 LinearLayoutCompat 。但在代码中,我通过使用 LinearLayout.LayoutParams 而不是 LinearLayoutCompat.LayoutParams 来达到参数。因此,当我将其更改为 LinearLayoutCompat.LayoutParams 时,它按预期工作。

in my case i had used LinearLayoutCompat in xml. But in the code i reached the params by using LinearLayout.LayoutParams instead of LinearLayoutCompat.LayoutParams. so when i changed it to LinearLayoutCompat.LayoutParams it worked as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文