【问题标题】:Setting height of a gridView item as double of width将 gridView 项目的高度设置为宽度的两倍
【发布时间】:2015-05-22 07:51:14
【问题描述】:

网格项的宽度会随着屏幕尺寸的变化而变化。 我需要将网格项的高度设置为网格项宽度的两倍。 即如果网格的宽度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;
}

【问题讨论】:

    标签: android android-layout gridview android-xml


    【解决方案1】:

    这可以通过在适配器的 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;
        }
    

    祝你好运!

    【讨论】:

    • @JITHINGOPAL 你能用你的整个 getView() 更新你的问题吗?我不确定您是返回 textview 还是它位于某种包装器中。我今天正在使用上面的代码并且它正在工作。所以也许我理解错了这个问题。但是更多的代码会有所帮助。
    • 返回的不是确切的宽度。它以像素为单位,我尝试直接将其转换为 dp,但无法正常工作
    【解决方案2】:

    创建一个继承 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。

    【讨论】:

    • 谢谢你,但在我的情况下这不切实际。我需要将项目更改为 imageView、LinearLayout 等任何其他解决方案吗?
    • 除了TextView,您还可以扩展任何其他类:ImageView、LinearLayout 等...
    猜你喜欢
    • 1970-01-01
    • 2017-11-14
    • 2020-03-19
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    相关资源
    最近更新 更多