我有一个自定义LinearLayout,它已添加到父布局。该父级布局可以更改大小,因此,我在听以下内容的更改:
public void onSizeChanged(int w, int h, int oldw, int oldh) {...}
在此方法中,根据新的大小,我使用以下方法隐藏或显示LinearLayout的某些成员:
setVisibility(GONE/VISIBLE);
但是,布局不会自动更新。调整父布局的大小,但不更新LL。但是,单击组件后布局不会更新,这会将我带到其他应用程序并返回到屏幕。
我尝试通过以下方式强制重新绘制:
requestLayout();
forceLayout();
invalidate();
但这不起作用。关于我可能在哪里弄错的任何想法?
编辑:代码示例
LinearLayout
public class MyLayout extends LinearLayout {
TextView tv1, tv2, tv3;
ImageView iv1;
public MyLayout(Context context) {
super(getContext());
setOrientation(VERTICAL);
tv1 = new TextView(getContext());
tv2 = new TextView(getContext());
tv3 = new TextView(getContext());
LinearLayout.LayoutParams tvParams = new LayoutParams(MATCH_PARENT, WRAP_CONTENT);
iv1 = new ImageView(getContext());
LayoutParams ivParams = new LayoutParams(MATCH_PARENT, 0);
ivParams.weight = 1;
addView(tv1, tvParams);
addView(iv1, ivParams);
addView(tv2, tvParams);
addView(tv3, tvParams);
}
public void onSizeChanged(int w, int h, int oldw, int oldh) {
int heightDp = (int) (h / DENSITY);
if (heightDp <= 250) {
tv1.setVisibility(GONE);
tv2.setVisibility(GONE);
tv3.setVisibility(GONE);
} else if (heightDp <= 350) {
tv1.setVisibility(VISIBLE);
tv2.setVisibility(VISIBLE);
tv3.setVisibility(GONE);
} else {
tv1.setVisibility(VISIBLE);
tv2.setVisibility(VISIBLE);
tv3.setVisibility(VISIBLE);
}
}
}