2015-05-22 55 views
0

當屏幕大小更改時,網格項目的寬度將會更改。 我需要將網格項目的高度設置爲網格項目寬度的兩倍。 也就是說,如果網格的寬度item=25dp高度必須是50dp將gridView項目的高度設置爲寬度的兩倍

這是我的適配器

@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

這可以通過編程方式更改視圖項目大小來實現的getView功能可按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; 
    } 

祝你好運!

+0

謝謝korrekorre,但那不太好。 –

+0

@JITHINGOPAL你可以用你的整個getView()來更新你的問題嗎?我不確定你是否返回了textview,或者它是否位於某種包裝中。我正在使用上面的代碼,它正在工作。所以也許我理解這個問題是錯誤的。但更多的代碼會有所幫助。 – korrekorre

+0

它不是確切的寬度是返回。它的像素,我試圖直接將其轉換爲DP也,但不會工作 –

0

創建一個子類(例如DoubleWidthTextView)延伸的TextView這樣:

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的。

+0

謝謝,但這是不實際的在我的情況。我需要將項目更改爲imageView,LinearLayout等任何其他解決方案? –

+0

而不是'TextView',你可以擴展任何其他類:ImageView,LinearLayout等... – Karim