当屏幕尺寸发生变化时,网格项的宽度将会改变。
我需要将网格项的高度设置为网格项宽度的两倍。
也就是说,如果网格item=25dp
高度的宽度必须为50dp
。
这是我的适配器的getView功能
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView no=new TextView(context);
no.setText(position+"");
// I need to set double of width instead of 150
no.setLayoutParams(new FrameLayout.LayoutParams(android.widget.FrameLayout.LayoutParams.FILL_PARENT,150));
}
更新.... 我的完整getView
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos=position;
View layout=null;
itemWidth=gridView.getColumnWidth();
layout=layout.inflate(context, R.layout.exam_grid_item, null);
TextView examName=(TextView) layout.findViewById(R.id.examNameTextView);
TextView examStatus=(TextView) layout.findViewById(R.id.textViewExamStatus);
LinearLayout itemContainer=(LinearLayout)layout.findViewById(R.id.itemContainar);
itemContainer.setLayoutParams(new FrameLayout.LayoutParams(android.widget.FrameLayout.LayoutParams.WRAP_CONTENT,itemWidth*2));
ImageView statusImage=(ImageView)layout.findViewById(R.id.examStatusImageView);
examName.setText("exam name"+itemHeight+"\n"+itemWidth);
statusImage.setImageResource(R.drawable.tic);
examStatus.setText("Attended");
return layout;
}
答案 0 :(得分:0)
这可以通过在适配器中的getView()中以编程方式更改视图项大小来实现。我自己做过。
这样的事情应该有效:
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
....
// Get item width by using getColumnWidth.
// By doing this you support dynamic column width in grid.
final int columnWidth = gridView.getColumnWidth();
// Set your views height here
final int columnHeight = 50; // This is in px
AbsListView.LayoutParams lp = (AbsListView.LayoutParams)view.getLayoutParams();
lp.width = columnWidth;
lp.height = columnHeight;
view.setLayoutParams(lp);
return view;
}
祝你好运!
答案 1 :(得分:0)
创建一个扩展TextView的子类(例如DoubleWidthTextView):
public class DoubleWidthTextView extends TextView {
...
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
int height = 2 * width;
setMeasuredDimension(width, height);
}
...
}
并使用它而不是TextView。