我在Framelayout內部有一個Linear layout。我想要做的是基於屏幕大小以編程方式向線性佈局添加一些邊距。
無法在Framelayout內設置Linearlayout參數
<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>
而且我的活動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);
我開始活動
E/AndroidRuntime時出現錯誤: 致命異常:主要 過程:COM .example.aba.webcampstest,PID:11723 java.lang.ClassCastException:a ndroid.widget.LinearLayout $ LayoutParams不能投到 android.widget.FrameLayout $ LayoutParams at android.widget.FrameLayout.onMeasure(FrameLayout.java:311) at android.view.View.measure(View.java:17495 ) 在android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1779) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 在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 ) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 在android.view.View.measure(View.java:17495) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363) 在機器人.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.V iewRootImpl.performMeasure(ViewRootImpl.java:2285) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1396) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1595) 在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。 Chronographer.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(手在android.app.ActivityThread.main(ActivityThread。)上的android.os.Looper.loop(Looper.java:146) 上的android.os.Handler.dispatchMessage(Handler.java:95) 。 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) 在dalvik.system.NativeStart.main(本機方法)
可能是什麼問題呢?
你應該先設置你的父母的佈局參數,然後它的child..in你的情況下,父親是#FrameLayout視圖activity_main的 –
LayoutParametres(的FrameLayout )已經在xml中設置了。就像mainContainerLayout的params一樣,但Coder想要在運行時改變它們。 – Goran