【问题标题】:Android: dynamically set the position of button into center of screen in RelativeLayoutAndroid:在RelativeLayout中动态设置按钮的位置到屏幕的中心
【发布时间】:2021-01-09 19:59:58
【问题描述】:

关于动态设置按钮的位置有很多问题,但我只想将按钮的位置更改为屏幕中心。我的布局是RelativeLayout。

更新:

一开始,myButton 在 XML 中的位置被设置为:

<Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Menu" />

任何建议将不胜感激

【问题讨论】:

    标签: android position android-relativelayout


    【解决方案1】:

    看下面的例子(点击按钮,它会被移动到中心)。

    activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <Button
            android:id="@+id/my_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:text="Menu" />
    
    </RelativeLayout>
    

    MainActivity.java

    public class MainActivity extends Activity {
        private View mView;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mView = findViewById(R.id.my_view);
            mView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ViewGroup.LayoutParams vg_lp = mView.getLayoutParams();
    
                    // Make sure we are in RelativeLayout
                    if (vg_lp instanceof RelativeLayout.LayoutParams) {
                        RelativeLayout.LayoutParams rl_lp = new RelativeLayout.LayoutParams(vg_lp);
                        rl_lp.addRule(RelativeLayout.CENTER_IN_PARENT);
                        mView.setLayoutParams(rl_lp);
                    }
                }
            });
        }
    }
    

    【讨论】:

    • 我将当前视图指定为 my_view,但按钮仍然没有移动。它与上面的 XML 保持在同一个位置
    • @DavidNg,我已经编辑了我的答案并添加了完整的代码清单。这个对我有用。再试一次或发布您的完整代码清单。
    【解决方案2】:

    试试:

          ......
          RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) button.getLayoutParams();
          layoutParams.leftMargin = parentLayout.getWidth()/2;
          layoutParams.topMargin = parentLayout.getHeight()/2;
          button.setLayoutParams(layoutParams);
          ...... 
    

    【讨论】:

    • 我设置:RelativeLayout parentLayout=(RelativeLayout)findViewById(R.layout.mylayout);使用您的代码,我的程序崩溃了。我错了吗?
    【解决方案3】:

    试试这个:

       RelativeLayout RL = (RelativeLayout)findViewById(R.id.relative_layout);
       RL.gravity(Gravity.CENTER);
    

    这样,RL 的所有子视图都将居中;

       Button anyButton = new Button(this);
       RL.addSubview(anyButton);//this genereted button will be in center as well
    

    【讨论】:

      【解决方案4】:

      试试这个,

      RelativeLayout my_layout = new RelativeLayout(this);
      my_layout.setId(some_value);
      Button my_btn = new Button(this);
      RelativeLayout.LayoutParams my_params = new RelativeLayout.LayoutParams(btn_height,btn_width);
      my_params.addRule(RelativeLayout.CENTER_IN_PARENT,my_layout.getId());
      my_layout.addView(my_btn,my_params);
      

      【讨论】:

        【解决方案5】:

        将这 2 行添加到要在 xml 文件中居中的组件:

        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-20
          • 1970-01-01
          • 2015-01-09
          相关资源
          最近更新 更多