Domanda

I want to adjust the margin of my Layout when hide some elements so I have tried this code

RelativeLayout mainLayout;
LinearLayout innerLayout;
public void hideInfoarea(){
    mainLayout = (RelativeLayout) findViewById(R.id.mainContainer);
    innerLayout = (LinearLayout) findViewById(R.id.innerLayout);
    mainContainer.removeView(infoLayout);

    //adjust margins
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)innerLayout.getLayoutParams();
    params.setMargins(10, 0, 0, 10);
    innerLayout.setLayoutParams(params);
}

Initially margins of innerLayout are 10,0,0,100 and in my intention this should change the margins in 10, 0,0,10 after infoLayout remotion, but unfortunately I get the follow error

 java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
            at com.myapp.test.Myactivity.hideInfoarea(Myactivity.java:371)
            at com.myapp.test.Myactivity.switchBoxStatus(Myactivity.java:360)
            at com.myapp.test.Myactivity.onOptionsItemSelected(Myactivity.java:256)
            at android.app.Activity.onMenuItemSelected(Activity.java:2600)
            at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:372)
            at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1012)
            at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
            at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
            at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
            at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:546)
            at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            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:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

innerLayout is a LinearLayout in my xml, not a RelativeLayout. so I don't understand the cause of the issue

È stato utile?

Soluzione

As you can read android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams. @dcharms is right, you should do:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) innerLayout.getLayoutParams();  

Because, the parent view which contains your innerLayout is a RelativeLayout not a LinearLayout.
Let me know if you succeed.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top