recyclerview 中的项目设置 layoutparams 错误
Items in recyclerview setting layoutparams wrong
我想要在我的通知应用程序中:
标题存在,文本不存在 = 标题应垂直居中
文本存在,标题不存在 = 文本应垂直居中
我的适配器是这样的:
public View view_title, view_text
public ViewHolder(View v) {
super(v);
...
view_title = v.findViewById(R.id.title);
view_text = v.findViewById(R.id.text);
title = (TextView) v.findViewById(R.id.title);
text = (TextView) v.findViewById(R.id.text);
...
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.title.setText(utilsM.getTitleByPos("tasks", position));
holder.text.setText(utilsM.getNoticeByPos("tasks", position));
if ( ( holder.title.getText().length() > 0) && (holder.text.getText().length() == 0) ) {
holder.view_title.setVisibility(View.VISIBLE);
holder.view_text.setVisibility(View.INVISIBLE);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.view_title.getLayoutParams();
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
holder.view_title.setLayoutParams(params);
debug("title: " + holder.title.getText());
debug("text: " + holder.text.getText());
} else if ( ( holder.text.getText().length() > 0) && (holder.title.getText().length() == 0) ) {
holder.view_title.setVisibility(View.INVISIBLE);
holder.view_text.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) holder.view_text.getLayoutParams();
params2.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
holder.view_text.setLayoutParams(params2);
} else {
holder.view_title.setVisibility(View.VISIBLE);
holder.view_text.setVisibility(View.VISIBLE);
}
我发现我可以在我的代码中设置 "android:layout_centerVertical="true":
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.view_title.getLayoutParams();
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
holder.view_title.setLayoutParams(params);
一切正常。
但有时确实会以标题或文本为中心的通知,尽管标题和文本都存在。怎么样?
看起来像这样:
抱歉我的英语不好 :D
有什么建议吗?
您忘记删除两个文本的最后 else
块中的 CenterVertical
属性。并且不要忘记对齐您的文本以免相互重叠。
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.view_title.getLayoutParams();
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) holder.view_text.getLayoutParams();
if ( ( holder.title.getText().length() > 0) && (holder.text.getText().length() == 0) ) {
holder.view_title.setVisibility(View.VISIBLE);
holder.view_text.setVisibility(View.INVISIBLE);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
holder.view_title.setLayoutParams(params);
debug("title: " + holder.title.getText());
debug("text: " + holder.text.getText());
} else if ( ( holder.text.getText().length() > 0) && (holder.title.getText().length() == 0) ) {
holder.view_title.setVisibility(View.INVISIBLE);
holder.view_text.setVisibility(View.VISIBLE);
params2.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
holder.view_text.setLayoutParams(params2);
} else {
// YOU NEED TO RESET YOUR 2 LAYOUT PARAMS HERE
params.addRule(RelativeLayout.CENTER_VERTICAL, 0);
params2.addRule(RelativeLayout.CENTER_VERTICAL, 0);
// .....
// align your texts here
// ....
holder.view_title.setLayoutParams(params);
holder.view_text.setLayoutParams(params2);
holder.view_title.setVisibility(View.VISIBLE);
holder.view_text.setVisibility(View.VISIBLE);
}
要删除规则,请使用:
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, 0);
或(对于API级别>=17)
layoutParams.removeRule(RelativeLayout.CENTER_VERTICAL);
在代码中,
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) submitCashbackFailedViewHolder.contentViewRlly.getLayoutParams();
layoutParams.height = LinearLayout.LayoutParams.MATCH_PARENT;
layoutParams.width = UiUtil.dpToPx(mContext,UiUtil.px2dip(UiUtil.getScreenWidth()) - 23);
问题是
layoutParams.height
需要像素。
但我用 dp 来设置它
我想要在我的通知应用程序中: 标题存在,文本不存在 = 标题应垂直居中 文本存在,标题不存在 = 文本应垂直居中
我的适配器是这样的:
public View view_title, view_text
public ViewHolder(View v) {
super(v);
...
view_title = v.findViewById(R.id.title);
view_text = v.findViewById(R.id.text);
title = (TextView) v.findViewById(R.id.title);
text = (TextView) v.findViewById(R.id.text);
...
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.title.setText(utilsM.getTitleByPos("tasks", position));
holder.text.setText(utilsM.getNoticeByPos("tasks", position));
if ( ( holder.title.getText().length() > 0) && (holder.text.getText().length() == 0) ) {
holder.view_title.setVisibility(View.VISIBLE);
holder.view_text.setVisibility(View.INVISIBLE);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.view_title.getLayoutParams();
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
holder.view_title.setLayoutParams(params);
debug("title: " + holder.title.getText());
debug("text: " + holder.text.getText());
} else if ( ( holder.text.getText().length() > 0) && (holder.title.getText().length() == 0) ) {
holder.view_title.setVisibility(View.INVISIBLE);
holder.view_text.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) holder.view_text.getLayoutParams();
params2.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
holder.view_text.setLayoutParams(params2);
} else {
holder.view_title.setVisibility(View.VISIBLE);
holder.view_text.setVisibility(View.VISIBLE);
}
我发现我可以在我的代码中设置 "android:layout_centerVertical="true":
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.view_title.getLayoutParams();
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
holder.view_title.setLayoutParams(params);
一切正常。 但有时确实会以标题或文本为中心的通知,尽管标题和文本都存在。怎么样?
看起来像这样:
抱歉我的英语不好 :D 有什么建议吗?
您忘记删除两个文本的最后 else
块中的 CenterVertical
属性。并且不要忘记对齐您的文本以免相互重叠。
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.view_title.getLayoutParams();
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) holder.view_text.getLayoutParams();
if ( ( holder.title.getText().length() > 0) && (holder.text.getText().length() == 0) ) {
holder.view_title.setVisibility(View.VISIBLE);
holder.view_text.setVisibility(View.INVISIBLE);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
holder.view_title.setLayoutParams(params);
debug("title: " + holder.title.getText());
debug("text: " + holder.text.getText());
} else if ( ( holder.text.getText().length() > 0) && (holder.title.getText().length() == 0) ) {
holder.view_title.setVisibility(View.INVISIBLE);
holder.view_text.setVisibility(View.VISIBLE);
params2.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
holder.view_text.setLayoutParams(params2);
} else {
// YOU NEED TO RESET YOUR 2 LAYOUT PARAMS HERE
params.addRule(RelativeLayout.CENTER_VERTICAL, 0);
params2.addRule(RelativeLayout.CENTER_VERTICAL, 0);
// .....
// align your texts here
// ....
holder.view_title.setLayoutParams(params);
holder.view_text.setLayoutParams(params2);
holder.view_title.setVisibility(View.VISIBLE);
holder.view_text.setVisibility(View.VISIBLE);
}
要删除规则,请使用:
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, 0);
或(对于API级别>=17)
layoutParams.removeRule(RelativeLayout.CENTER_VERTICAL);
在代码中,
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) submitCashbackFailedViewHolder.contentViewRlly.getLayoutParams();
layoutParams.height = LinearLayout.LayoutParams.MATCH_PARENT;
layoutParams.width = UiUtil.dpToPx(mContext,UiUtil.px2dip(UiUtil.getScreenWidth()) - 23);
问题是
layoutParams.height
需要像素。
但我用 dp 来设置它