android setLayoutParams不生效问题
核心更改:RelativeLayout.END_OF 改为:RelativeLayout.ALIGN_LEFT,
·
不生效代码:
public void initSpeedView(boolean hasPark) {
lightNavSpeedView = new LightNavSpeedView(getNaviActivity());
RelativeLayout.LayoutParams lightNavSpeedViewLayoutParams = getWrapLayoutParams();
lightNavSpeedViewLayoutParams
.addRule(RelativeLayout.END_OF, lightNavGuideView.getView().getId());
int leftMargin = hasPark ?
getDimen(R.dimen.light_nav_speed_view_margin_left)
- (getDimen(R.dimen.navi_end_card_panel_width)
- getDimen(R.dimen.light_navi_route_guide_panel_width))
:getDimen(R.dimen.light_nav_speed_view_margin_left);
lightNavSpeedViewLayoutParams.setMargins(
leftMargin,
getDimen(R.dimen.light_nav_speed_view_margin_top),
getDimen(R.dimen.light_nav_speed_view_margin_right),
getDimen(R.dimen.light_nav_speed_view_margin_bottom));
if (mViewGroup.findViewById(R.id.linearLightNavSpeedRootView) != null) {
mViewGroup.findViewById(R.id.linearLightNavSpeedRootView).setLayoutParams(lightNavSpeedViewLayoutParams);
} else {
mViewGroup.addView(lightNavSpeedView.getView(), lightNavSpeedViewLayoutParams);
}
}
解决方式分为二种:
第一种使用remove然后在add的方式:
if (mViewGroup.findViewById(R.id.linearLightNavSpeedRootView) != null) {
mViewGroup.removeView(mViewGroup.findViewById(R.id.linearLightNavSpeedRootView));
}
mViewGroup.addView(lightNavSpeedView.getView(), lightNavSpeedViewLayoutParams);
public void initSpeedView(boolean hasPark) {
lightNavSpeedView = new LightNavSpeedView(getNaviActivity());
RelativeLayout.LayoutParams lightNavSpeedViewLayoutParams = getWrapLayoutParams();
if (ChannelUtil.isC1YCChannel()) {
lightNavSpeedViewLayoutParams.width = getDimen(R.dimen.navi_rg_interval_camera_bg_size);
lightNavSpeedViewLayoutParams.height = getDimen(R.dimen.navi_rg_interval_camera_bg_size);
}
lightNavSpeedViewLayoutParams
.addRule(RelativeLayout.END_OF, lightNavGuideView.getView().getId());
int leftMargin = hasPark?
getDimen(R.dimen.light_nav_speed_view_margin_left)
- (getDimen(R.dimen.navi_end_card_panel_width)
- getDimen(R.dimen.light_navi_route_guide_panel_width))
:getDimen(R.dimen.light_nav_speed_view_margin_left);
lightNavSpeedViewLayoutParams.setMargins(
leftMargin,
getDimen(R.dimen.light_nav_speed_view_margin_top),
getDimen(R.dimen.light_nav_speed_view_margin_right),
getDimen(R.dimen.light_nav_speed_view_margin_bottom));
if (mViewGroup.findViewById(R.id.linearLightNavSpeedRootView) != null) {
mViewGroup.removeView(mViewGroup.findViewById(R.id.linearLightNavSpeedRootView));
}
mViewGroup.addView(lightNavSpeedView.getView(), lightNavSpeedViewLayoutParams);
}
第二种更换xml布局把右对齐换位做对齐:
核心更改:RelativeLayout.END_OF 改为:RelativeLayout.ALIGN_LEFT,
lightNavSpeedViewLayoutParams .addRule(RelativeLayout.ALIGN_LEFT, lightNavGuideView.getView().getId());
private void initSpeedView() {
lightNavSpeedView = new LightNavSpeedView(getNaviActivity());
RelativeLayout.LayoutParams lightNavSpeedViewLayoutParams = getWrapLayoutParams();
if (ChannelUtil.isC1YCChannel()) {
lightNavSpeedViewLayoutParams.width = getDimen(R.dimen.navi_rg_interval_camera_bg_size);
lightNavSpeedViewLayoutParams.height = getDimen(R.dimen.navi_rg_interval_camera_bg_size);
}
lightNavSpeedViewLayoutParams
.addRule(RelativeLayout.ALIGN_LEFT, lightNavGuideView.getView().getId());
lightNavSpeedViewLayoutParams.setMargins(
getDimen(R.dimen.light_nav_speed_view_margin_left)
+ getDimen(R.dimen.light_navi_route_guide_panel_width),
getDimen(R.dimen.light_nav_speed_view_margin_top),
getDimen(R.dimen.light_nav_speed_view_margin_right),
getDimen(R.dimen.light_nav_speed_view_margin_bottom));
mViewGroup.addView(lightNavSpeedView.getView(), lightNavSpeedViewLayoutParams);
}
更多推荐
已为社区贡献9条内容
所有评论(0)