The width of the grid item will change when the screen size changes. I need to set the height of the grid item as double of the width of the grid item. That is , if the width of grid item=25dp
height must be 50dp
.
当屏幕尺寸改变时,网格项的宽度将改变。我需要将网格项的高度设置为网格项宽度的两倍。也就是说,如果网格项的宽度= 25dp,则高度必须为50dp。
This is getView funciton of my Adapter
这是我的适配器的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));
}
Updating.... My Complete getView
正在更新....我的完整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;
}
2 个解决方案
#1
0
This can be achieved by changing the view item size programmatically in getView() in your adapter. I've done it myself.
这可以通过在适配器中的getView()中以编程方式更改视图项大小来实现。我自己做了。
Something like this should work:
像这样的东西应该工作:
@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;
}
Good luck!
#2
0
Create a subclass (for example DoubleWidthTextView) that extends TextView as such:
创建一个扩展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);
}
...
}
And use it instead of TextView.
并使用它而不是TextView。
#1
0
This can be achieved by changing the view item size programmatically in getView() in your adapter. I've done it myself.
这可以通过在适配器中的getView()中以编程方式更改视图项大小来实现。我自己做了。
Something like this should work:
像这样的东西应该工作:
@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;
}
Good luck!
#2
0
Create a subclass (for example DoubleWidthTextView) that extends TextView as such:
创建一个扩展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);
}
...
}
And use it instead of TextView.
并使用它而不是TextView。