Java – Android. Setting left / right margins does not work

This is strange, but setting left or right margins in Java code does not work on some devices / platforms

This is the code. Simple XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    >

    <View
        android:id="@+id/myView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:background="#00ff00" />

</FrameLayout>

Java:

@Override
public void onConfigurationChanged (Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);

    if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)findViewById(R.id.myView).getLayoutParams();
        params.leftMargin = 50;
        params.rightMargin = 50;
        params.topMargin = 50;
        params.bottomMargin = 50;
        findViewById(R.id.myView).requestLayout();
    }
}

So I have a green view in FrameLayout. I change the view margin when rotating. (my activities include Android: configchanges = "orientation | screensize")

The results are as follows:

Samsung Galaxy S 3 (Android 4.1.2) (working normally)

Sony Ericsson Xperia arc s (Android 4.0.4) (very strange!)

As you can see, the code works normally on Android 4.1.2. And the left and right margins are not set on Android 4.0.4, but the top margin is set. Why?

I have tested it on the simulator. All platforms > = Android 4 can. On Android 2.3, the margin is not set at all, even the upper margin

Who knows how to deal with it? How do I change the margin in my code so that it runs on all platforms?

resolvent:

Neither Sony Ericsson Xperia arc s (Android 4.0.4) nor Sony lt26ii (Android 4.0.4) works. I have no choice but to replace it with relativelayout

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>