【问题标题】:layout setMargins doesn't work in landscape mode布局 setMargins 在横向模式下不起作用
【发布时间】:2013-05-06 11:12:44
【问题描述】:

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

    }

我犯了什么错误吗? 端口视图

风景

在横向视图中更改当前项目后

【问题讨论】:

  • 在横向模式下边距工作正常,但它不能适应屏幕..当布局加载第一次时间比它适合屏幕但触发点击事件后它不能保持适合屏幕。 .
  • 问题出现在第三张图片中...点击任何项目后。

标签: android android-layout margins


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多