王学岗RelativeLayout.LayoutParams 的addRule()方法不起作用


   abl_scroll.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            

                if (Math.abs(verticalOffset) == tl_toolbar.getHeight()) {
                    RelativeLayout.LayoutParams layoutParams_tbl_three = (RelativeLayout.LayoutParams) tbl_three.getLayoutParams();
             
                    layoutParams_tbl_three.getRules()[RelativeLayout.CENTER_IN_PARENT] = 0;
                    layoutParams_tbl_three.getRules()[RelativeLayout.ALIGN_PARENT_LEFT] = 0;
                    layoutParams_tbl_three.addRule(RelativeLayout.CENTER_IN_PARENT);
                    tbl_three.setLayoutParams(layoutParams_tbl_three);
                    tv_personal_team_numbers.setVisibility(View.GONE);
                    tv_share2.setVisibility(View.VISIBLE);
                    tv_share2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            shares();
                        }
                    });
                    iv_back2.setVisibility(View.VISIBLE);
                    iv_back2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            GroupUserActivity.this.finish();
                        }
                    });
                } else {
                    RelativeLayout.LayoutParams layoutParams_tbl_three = (RelativeLayout.LayoutParams) tbl_three.getLayoutParams();
 
                    layoutParams_tbl_three.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    tbl_three.setLayoutParams(layoutParams_tbl_three);
                    iv_back2.setVisibility(View.GONE);
                    tv_personal_team_numbers.setVisibility(View.VISIBLE);
                    tv_personal_team_numbers.setClickable(false);
                    tv_personal_team_numbers.setText("团队成员(" + memberCount + ")");
                    tv_share2.setVisibility(View.GONE);
                }

            }
        });

abl_scroll是一个AppBarLayout,这个方法的作用是根据它的滑动距离改变 tbl_three(是一个TabLayout)的item显示的方位,上滑动到顶的时候居中,不到顶的时候位于左侧。但是layoutParams_tbl_three.addRule(RelativeLayout.CENTER_IN_PARENT);一直不起作用。在它的前面添加了layoutParams_tbl_three.getRules()[RelativeLayout.CENTER_IN_PARENT] = 0; layoutParams_tbl_three.getRules()[RelativeLayout.ALIGN_PARENT_LEFT] = 0;
有了这句代码后才起作用。

你可能感兴趣的:(王学岗RelativeLayout.LayoutParams 的addRule()方法不起作用)