gpt4 book ai didi

java - 无法在 Framelayout 中设置 Linearlayout 参数

转载 作者:太空宇宙 更新时间:2023-11-03 13:12:13 24 4
gpt4 key购买 nike


我在 Framelayout 中有一个线性布局。我想做的是根据屏幕尺寸以编程方式向线性布局添加一些边距。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none">
<FrameLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.aba.webcampstest.MainActivity"
android:orientation="vertical">

<ImageView
android:id="@+id/headerImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:layout_marginTop="0dp"
android:scaleType="centerCrop"/>

<LinearLayout
android:id="@+id/mainContainerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

//oher stuffs
</LinearLayout>
</FrameLayout >
</ScrollView>

我的 Activity onCreate 方法包含

LinearLayout containerLayout = (LinearLayout) findViewById(R.id.mainContainerLayout);
int marginTopBottom = dpToPx((int)((dpHeight*8)/100),(int)density); //8%
int marginLeftRight = dpToPx((int)((dpWidth*6)/100),(int)density); //6%

LinearLayout.LayoutParams languageContainerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
languageContainerLayoutParams.setMargins(marginLeftRight,marginTopBottom,marginLeftRight,marginTopBottom);
containerLayout.setLayoutParams(languageContainerLayoutParams);

启动Activity时出现错误

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.aba.webcampstest, PID: 11723 java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams at android.widget.FrameLayout.onMeasure(FrameLayout.java:311) at android.view.View.measure(View.java:17495) at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1779) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.widget.ScrollView.onMeasure(ScrollView.java:476) at android.view.View.measure(View.java:17495) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:17495) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410) at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) at android.view.View.measure(View.java:17495) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2548) at android.view.View.measure(View.java:17495) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2285) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1396) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1595) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6637) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:814) at android.view.Choreographer.doCallbacks(Choreographer.java:614) at android.view.Choreographer.doFrame(Choreographer.java:584) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:800) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5602) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at dalvik.system.NativeStart.main(Native Method)

可能是什么问题?

最佳答案

setLayoutParams description :

Set the layout parameters associated with this view. These supply parameters to the parent of this view specifying how it should be arranged. There are many subclasses of ViewGroup.LayoutParams, and these correspond to the different subclasses of ViewGroup that are responsible for arranging their children.

LayoutParams 类型应该与您的 LinearLayout 的容器相关,在本例中为 FrameLayout.LayoutParams :

FrameLayout.LayoutParams languageContainerLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
languageContainerLayoutParams.setMargins(marginLeftRight,marginTopBottom,marginLeftRight,marginTopBottom);
containerLayout.setLayoutParams(languageContainerLayoutParams);

关于java - 无法在 Framelayout 中设置 Linearlayout 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42666096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com