1

我有一个静态布局,我在程序化动态中设置了边距。边距工作正常,但仅在纵向视图中。这是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>

我为保证金编写了以下代码



    Button btn_home,    btn_browse, btn_account,    btn_mybag,  btn_more;
    RelativeLayout  rl_home,    rl_browse,  rl_account, rl_mybag,   rl_more;
    LinearLayout LinearLayout1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.browse);


        btn_home=(Button) findViewById(R.id.btn_home);
        btn_browse=(Button) findViewById(R.id.btn_browse);
        btn_account=(Button) findViewById(R.id.btn_account);
        btn_mybag=(Button) findViewById(R.id.btn_mybag);
        btn_more=(Button) findViewById(R.id.btn_more);

        rl_home=(RelativeLayout) findViewById(R.id.rl_home);
        rl_browse=(RelativeLayout) findViewById(R.id.rl_browse);
        rl_account=(RelativeLayout) findViewById(R.id.rl_account);
        rl_mybag=(RelativeLayout) findViewById(R.id.rl_mybag);
        rl_more=(RelativeLayout) findViewById(R.id.rl_more);
        LinearLayout1=(LinearLayout) findViewById(R.id.LinearLayout1);
    }

    public void ActiveMenu(View v) {


        if (R.id.btn_home == v.getId()) {
            rl_home.setBackgroundResource(R.drawable.current_menu);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 0, 0, 0);
            rl_home.setLayoutParams(params);
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            params1.setMargins(0, 15, 0, 0);

            rl_browse.setBackgroundResource(R.drawable.menubackground);
            rl_browse.setLayoutParams(params1);
            rl_account.setBackgroundResource(R.drawable.menubackground);
            rl_account.setLayoutParams(params1);
            rl_mybag.setBackgroundResource(R.drawable.menubackground);
            rl_mybag.setLayoutParams(params1);
            rl_more.setBackgroundResource(R.drawable.menubackground);
            rl_more.setLayoutParams(params1);

        }

        if (R.id.btn_browse== v.getId()) {
            rl_browse.setBackgroundResource(R.drawable.current_menu);

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

            rl_home.setBackgroundResource(R.drawable.menubackground);
            rl_home.setLayoutParams(params1);
            rl_account.setBackgroundResource(R.drawable.menubackground);
            rl_account.setLayoutParams(params1);
            rl_mybag.setBackgroundResource(R.drawable.menubackground);
            rl_mybag.setLayoutParams(params1);
            rl_more.setBackgroundResource(R.drawable.menubackground);
            rl_more.setLayoutParams(params1);

        }

        if (R.id.btn_account == v.getId()) {

            rl_account.setBackgroundResource(R.drawable.current_menu);

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

            rl_browse.setBackgroundResource(R.drawable.menubackground);
            rl_browse.setLayoutParams(params1);
            rl_home.setBackgroundResource(R.drawable.menubackground);
            rl_home.setLayoutParams(params1);
            rl_mybag.setBackgroundResource(R.drawable.menubackground);
            rl_mybag.setLayoutParams(params1);
            rl_more.setBackgroundResource(R.drawable.menubackground);
            rl_more.setLayoutParams(params1);
        }

        if (R.id.btn_mybag == v.getId()) {
            rl_mybag.setBackgroundResource(R.drawable.current_menu);

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

            rl_browse.setBackgroundResource(R.drawable.menubackground);
            rl_browse.setLayoutParams(params1);
            rl_account.setBackgroundResource(R.drawable.menubackground);
            rl_account.setLayoutParams(params1);
            rl_home.setBackgroundResource(R.drawable.menubackground);
            rl_home.setLayoutParams(params1);
            rl_more.setBackgroundResource(R.drawable.menubackground);
            rl_more.setLayoutParams(params1);
        }

        if (R.id.btn_more== v.getId()) {
            rl_more.setBackgroundResource(R.drawable.current_menu);

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


            rl_browse.setBackgroundResource(R.drawable.menubackground);
            rl_browse.setLayoutParams(params1);
            rl_account.setBackgroundResource(R.drawable.menubackground);
            rl_account.setLayoutParams(params1);
            rl_home.setBackgroundResource(R.drawable.menubackground);
            rl_home.setLayoutParams(params1);
            rl_mybag.setBackgroundResource(R.drawable.menubackground);
            rl_mybag.setLayoutParams(params1);
        }

    }

是不是我犯了什么错误??端口视图 在此处输入图像描述

风景 在此处输入图像描述

在横向视图中更改当前项目后 在此处输入图像描述

4

1 回答 1

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);
于 2013-05-07T13:53:33.953 回答