layout setMargins在横向模式下不起作用

时间:2013-05-06 11:12:45

标签: android android-layout margins

我有一个静态布局,我在程序化动态中设置了边距。保证金工作正常但仅在纵向视图中。这是xml布局

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:gravity="center_vertical" >

    <RelativeLayout
        android:id="@+id/rl_home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/menubackground"
        android:layout_weight="1.0"
        android:gravity="center" >

        <Button
            android:id="@+id/btn_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/icon1"
            android:gravity="center"
            android:background="@null"
            android:onClick="ActiveMenu"
            android:text="@string/home"
            android:textColor="@color/White"
            android:textStyle="bold" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_browse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:background="@drawable/menubackground"
        android:gravity="center" >

        <Button
            android:id="@+id/btn_browse"
            android:background="@null"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/icon2"
            android:gravity="center"
            android:onClick="ActiveMenu"
            android:text="@string/browse"
            android:textColor="@color/White"
            android:textStyle="bold" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_account"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:background="@drawable/menubackground"
        android:gravity="center" >

        <Button
            android:background="@null"
            android:id="@+id/btn_account"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/icon3"
            android:gravity="center"
            android:onClick="ActiveMenu"
            android:text="@string/account"
            android:textColor="@color/White"
            android:textStyle="bold" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_mybag"
        android:background="@drawable/menubackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:gravity="center" >

        <Button
            android:background="@null"
            android:id="@+id/btn_mybag"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/icon4"
            android:gravity="center"
            android:onClick="ActiveMenu"
            android:text="@string/my_bag"
            android:textColor="@color/White"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_no_Of_items"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignRight="@+id/btn_mybag"
            android:layout_marginRight="0.0dip"
            android:background="@drawable/red_circle"
            android:gravity="center"
            android:text="1"
            android:textColor="@color/White"
            android:textSize="15.0dip"
            android:textStyle="bold"
            android:visibility="invisible" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_more"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:background="@drawable/menubackground"
        android:gravity="center" >

        <Button
            android:background="@null"
            android:id="@+id/btn_more"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/icon5"
            android:gravity="center"
            android:onClick="ActiveMenu"
            android:text="@string/more"
            android:textColor="@color/White"
            android:textStyle="bold" />
    </RelativeLayout>
</LinearLayout>

<LinearLayout android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:gravity="center_vertical" > <RelativeLayout android:id="@+id/rl_home" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/menubackground" android:layout_weight="1.0" android:gravity="center" > <Button android:id="@+id/btn_home" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@drawable/icon1" android:gravity="center" android:background="@null" android:onClick="ActiveMenu" android:text="@string/home" android:textColor="@color/White" android:textStyle="bold" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_browse" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:background="@drawable/menubackground" android:gravity="center" > <Button android:id="@+id/btn_browse" android:background="@null" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@drawable/icon2" android:gravity="center" android:onClick="ActiveMenu" android:text="@string/browse" android:textColor="@color/White" android:textStyle="bold" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_account" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:background="@drawable/menubackground" android:gravity="center" > <Button android:background="@null" android:id="@+id/btn_account" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@drawable/icon3" android:gravity="center" android:onClick="ActiveMenu" android:text="@string/account" android:textColor="@color/White" android:textStyle="bold" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_mybag" android:background="@drawable/menubackground" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:gravity="center" > <Button android:background="@null" android:id="@+id/btn_mybag" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@drawable/icon4" android:gravity="center" android:onClick="ActiveMenu" android:text="@string/my_bag" android:textColor="@color/White" android:textStyle="bold" /> <TextView android:id="@+id/tv_no_Of_items" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignRight="@+id/btn_mybag" android:layout_marginRight="0.0dip" android:background="@drawable/red_circle" android:gravity="center" android:text="1" android:textColor="@color/White" android:textSize="15.0dip" android:textStyle="bold" android:visibility="invisible" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_more" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:background="@drawable/menubackground" android:gravity="center" > <Button android:background="@null" android:id="@+id/btn_more" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@drawable/icon5" android:gravity="center" android:onClick="ActiveMenu" android:text="@string/more" android:textColor="@color/White" android:textStyle="bold" /> </RelativeLayout> </LinearLayout> 我为保证金编写了以下代码


我犯了什么错误? Portrate视图 enter image description here

横向视图enter image description here

在横向视图中更改当前项目后 enter image description here

1 个答案:

答案 0 :(得分:1)

最后我得到了解决方案。在我发布的旧代码中,我编写了这段代码

LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params1.setMargins(0, 15, 0, 0);

我在代码下方进行了更改..这解决了我的问题。

final LinearLayout.LayoutParams params1 = (LinearLayout.LayoutParams) rl_home
                .getLayoutParams();
params1.setMargins(0, 15, 0, 0);
rl_home.setLayoutParams(params1);