Android系统。设定左/右页边距不工作 [英] Android. Setting left/right margin doesn't work
问题描述
这是奇怪的,但在Java code设置左边或右边距并不能在一些设备/平台上工作。
This is strange, but setting left or right margin in Java code doesn't work on some devices/platforms.
下面是code。
简单的XML:
Here 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();
}
}
所以我有内部的FrameLayout一个绿色的观点。在旋转更改视图的保证金。 (我的活动有机器人:configChanges =方向|屏幕尺寸)
So I've got a green view inside FrameLayout. On rotate I change margin of View. (my activity has android:configChanges="orientation|screenSize")
下面是结果:
三星Galaxy S 3(4.1.2 Android版)(正常工作)
Samsung Galaxy S 3 (Android 4.1.2) (works fine)
索尼爱立信Xperia Arc S(的Android 4.0.4)(作品奇怪!)
Sony Ericsson Xperia Arc S (Android 4.0.4) (works strange!)
正如你可以在code看到正常运行于Android 4.1.2。而在Android 4.0.4左,右页边距没有设置,但上边距设置。为什么上面?
As you can see on the code works properly on Android 4.1.2. And on Android 4.0.4 left and right margins are not set, but top margin is set. Why top?
我已经在模拟器进行测试。所有平台> =安卓4.0工作正常。在Android 2.3的利润率都没有设置可言,甚至上边距。
I've tested on emulators. All platforms >= Android 4 works fine. On Android 2.3 margins are not set at all, even top margin.
有谁知道如何处理呢?如何更改code保证金所以它会在所有平台上工作?
Does anyone know how to deal with it? How to change margin in code so it would work on all platforms?
推荐答案
索尼爱立信Xperia Arc S(的Android 4.0.4)
索尼LT26ii(Android版4.0.4)
这两个不工作
我没有选择,只能与RelativeLayout的替换
Sony Ericsson Xperia Arc S (Android 4.0.4) Sony LT26ii (Android 4.0.4) both doesn't work I have no choice but replace it with RelativeLayout
这篇关于Android系统。设定左/右页边距不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!