我有一个ListView
,我列表中的每一行都是RelativeLayout
,ImageView
和TextView
。我想在滚动列表时动态更改RelativeLayout
的尺寸,这样第一行和最后一行宽度等于屏幕宽度,中间行宽度是屏幕宽度的一半。我可以使用以下代码动态更改TextView
的文本大小:
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//Calculate height and width based on visible count items and position
final int viewHeight = height // calculated based on row position;
final int viewWidth = width // calculated based on row position;
for (int i = 0; i < visibleItemCount; i++) {
final RelativeLayout layout = (RelativeLayout) listView.getChildAt(i);
final ImageView imageView = (ImageView) layout.findViewById(R.id.icon);
final TextView time = (TextView) layout.findViewById(R.id.time);
time.post(new Runnable() {
@Override
public void run() {
time.setTextSize(viewHeight);
}
});
}
}
但我想在滚动时动态更改整个行布局的尺寸。请检查下面的代码。任何帮助表示赞赏。
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//Calculate height and width based on visible count items and position
final int viewHeight = height // calculated based on row position;
final int viewWidth = width // calculated based on row position;
for (int i = 0; i < visibleItemCount; i++) {
final RelativeLayout layout = (RelativeLayout) listView.getChildAt(i);
setLayoutDimensions(layout,viewHeight, viewWidth);
}
}
private void setLayoutDimensions(final RelativeLayout layout, final int height, final int width){
RelativeLayout.LayoutParams adaptLayout = new RelativeLayout.LayoutParams(width, height);
layout.setLayoutParams(adaptLayout);
}
答案 0 :(得分:0)
尝试设置相对布局的宽度和高度,如下所示:
LayoutParams params = layout.getLayoutParams();
params.width= width;
params.width= height;
layout.setLayoutParams(params);
您可以使用TypedValue.applyDimension方法根据设备屏幕密度将宽度和高度转换为dp