使用LayoutParams以编程方式从FrameLayout中删除边距

时间:2015-04-25 23:21:07

标签: android relativelayout classcastexception android-framelayout

我有一个看起来像这样的FrameLayout:

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <FrameLayout
        android:layout_marginTop="56dp"
        android:layout_marginBottom="50dp"
        android:id="@+id/main_frag"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

          // other stuff


     </RelativeLayout>

然而,当我按照程序改变边距时,我得到一个ClassCastException。

        FrameLayout fl = (FrameLayout) findViewById(R.id.main_frag);
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) fl.getLayoutParams();
        params.setMargins(50, 0, 0, 0);
        fl.setLayoutParams(params);

以下是我的尝试方式:

        FrameLayout fl = (FrameLayout) findViewById(R.id.main_frag);
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) fl.getLayoutParams();
        params.setMargins(50, 0, 0, 0);
        fl.setLayoutParams(params);

以下是LogCat

04-25 16:18:08.385: E/AndroidRuntime(27682): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.---.---/com.------.---.MainFragmentActivity}: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
04-25 16:18:08.385: E/AndroidRuntime(27682):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
04-25 16:18:08.385: E/AndroidRuntime(27682):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
04-25 16:18:08.385: E/AndroidRuntime(27682):    at android.app.ActivityThread.access$800(ActivityThread.java:148)
04-25 16:18:08.385: E/AndroidRuntime(27682):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
04-25 16:18:08.385: E/AndroidRuntime(27682):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-25 16:18:08.385: E/AndroidRuntime(27682):    at android.os.Looper.loop(Looper.java:135)
04-25 16:18:08.385: E/AndroidRuntime(27682):    at android.app.ActivityThread.main(ActivityThread.java:5312)
04-25 16:18:08.385: E/AndroidRuntime(27682):    at java.lang.reflect.Method.invoke(Native Method)
04-25 16:18:08.385: E/AndroidRuntime(27682):    at java.lang.reflect.Method.invoke(Method.java:372)
04-25 16:18:08.385: E/AndroidRuntime(27682):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
04-25 16:18:08.385: E/AndroidRuntime(27682):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
04-25 16:18:08.385: E/AndroidRuntime(27682): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
04-25 16:18:08.385: E/AndroidRuntime(27682):    at com.---.---.MainFragmentActivity.onCreate(MainFragmentActivity.java:81)
04-25 16:18:08.385: E/AndroidRuntime(27682):    at android.app.Activity.performCreate(Activity.java:5953)
04-25 16:18:08.385: E/AndroidRuntime(27682):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
04-25 16:18:08.385: E/AndroidRuntime(27682):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)

我不明白RelativeLayout是如何参与的?

1 个答案:

答案 0 :(得分:3)

LayoutParams课程由家长决定,而不是孩子。影响LayoutParams的{​​{1}}为FrameLayout。影响RelativeLayout.LayoutParams孩子LayoutParams将是FrameLayout