android.widget.GridView.setLayoutParams()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(11.5k)|赞(0)|评价(0)|浏览(213)

本文整理了Java中android.widget.GridView.setLayoutParams()方法的一些代码示例,展示了GridView.setLayoutParams()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。GridView.setLayoutParams()方法的具体详情如下:
包路径:android.widget.GridView
类名称:GridView
方法名:setLayoutParams

GridView.setLayoutParams介绍

暂无

代码示例

代码示例来源:origin: TommyLemon/APIJSON

try {
  if (pictureNum >= 7) {
    gvMomentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, gridViewHeight));
  } else if (pictureNum >= 4) {
    gvMomentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, (gridViewHeight*2)/3));
  } else if (pictureNum >= 2) {
    gvMomentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, gridViewHeight / 3));
  } else {
    gvMomentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

代码示例来源:origin: stackoverflow.com

final GridView imageContainer = // your GridView
imageContainer.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() 
      {
      @Override
      public void onGlobalLayout() 
        {
        imageContainer.getViewTreeObserver().removeGlobalOnLayoutListener( this );
        View lastChild = imageContainer.getChildAt( imageContainer.getChildCount() - 1 );
        imageContainer.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, lastChild.getBottom() ) );
        }
      });

代码示例来源:origin: stackoverflow.com

GridView gridView = (GridView)inflater.inflate(R.layout.mygrid, null);
gridView.setLayoutParams(new GridView.LayoutParams(customValue, 
            LayoutParams.FILL_PARENT));
gridView.setNumColumns(someInt);
gridView.setAdapter (new MyCustomAdapter(this));

代码示例来源:origin: stackoverflow.com

private void resizeGridView(GridView gridView, int items, int columns) {
  ViewGroup.LayoutParams params = gridView.getLayoutParams();
  int oneRowHeight = gridView.getHeight();
  int rows = (int) (items / columns);
  params.height = oneRowHeight * rows;
  gridView.setLayoutParams(params);
}

代码示例来源:origin: stackoverflow.com

private void resizeGridView(GridView gridView, int items, int columns) {
  ViewGroup.LayoutParams params = gridView.getLayoutParams();
  int oneRowHeight = gridView.getHeight();
  int rows = (int) (items / columns);
  params.height = oneRowHeight * rows;
  gridView.setLayoutParams(params);
}

代码示例来源:origin: LRH1993/LiveCircle

private void setHeaderItemValues(HeaderViewHolder holder) {
  NewsChannelGridAdapter mAdapter = new NewsChannelGridAdapter(mTables, context);
  holder.mGridView.setAdapter(mAdapter);
  //解决GridView只显示一行的原因 主动设置GridView的高度
  ViewGroup.LayoutParams params = holder.mGridView.getLayoutParams();
  View view = mAdapter.getView(0, null, holder.mGridView);
  view.measure(0,0);
  int height =view.getMeasuredHeight();
  int totalHeight = holder.mGridView.getVerticalSpacing() * 2 + height * 2;
  params.height = totalHeight;
  holder.mGridView.setLayoutParams(params);
}

代码示例来源:origin: mouxuefei/KotlinMvpExample

/**
 * 动态设置gridView的高度
 *
 * @param gridView
 * @param columns  列数
 */
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public static void setGridViewHeight(GridView gridView, int columns) {
  ListAdapter adapter = gridView.getAdapter();
  int count = adapter.getCount();
  int row = count / columns;
  row = (count % columns) == 0 ? row : (row + 1);
  int totalHeight = 0;
  for (int i = 0; i < row; i++) {
    View view = adapter.getView(i, null, gridView);
    view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    totalHeight += view.getMeasuredHeight();
  }
  ViewGroup.LayoutParams layoutParams = gridView.getLayoutParams();
  layoutParams.height = totalHeight + (gridView.getVerticalSpacing() * (row - 1));
  gridView.setLayoutParams(layoutParams);
}

代码示例来源:origin: stackoverflow.com

// create a gridview
GridView gridView= new GridView(this);

gridView.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
gridView.setNumColumns(set number of columns here);
gridView.setColumnWidth(GridView.AUTO_FIT);  
gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);  
gridView.setAdapter(adapter);

代码示例来源:origin: stackoverflow.com

private static void resizeGridView(GridView gridView, int rows) {
  int measuredHeight = gridView.getMeasuredHeight();
  ViewGroup.LayoutParams params = gridView.getLayoutParams();
  params.height = measuredHeight * rows;
  gridView.setLayoutParams(params);
  gridView.requestLayout();
}

代码示例来源:origin: bilibili/BiliShare

protected static GridView createShareGridView(final Context context, AdapterView.OnItemClickListener onItemClickListener) {
  GridView grid = new GridView(context);
  ListAdapter adapter = new ArrayAdapter<ShareTarget>(context, 0, shareTargets) {
    // no need scroll
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.bili_socialize_shareboard_item, parent, false);
      view.setBackgroundDrawable(null);
      ImageView image = (ImageView) view.findViewById(R.id.bili_socialize_shareboard_image);
      TextView platform = (TextView) view.findViewById(R.id.bili_socialize_shareboard_pltform_name);
      ShareTarget target = getItem(position);
      image.setImageResource(target.iconId);
      platform.setText(target.titleId);
      return view;
    }
  };
  grid.setNumColumns(-1);
  grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
  grid.setColumnWidth(context.getResources().getDimensionPixelSize(R.dimen.bili_socialize_shareboard_size));
  grid.setLayoutParams(new ViewGroup.LayoutParams(-1, -2));
  grid.setSelector(R.drawable.bili_socialize_selector_item_background);
  grid.setAdapter(adapter);
  grid.setOnItemClickListener(onItemClickListener);
  return grid;
}

代码示例来源:origin: stackoverflow.com

public static void setHeightDynamically(GridView listView) {
   ListAdapter listAdapter = listView.getAdapter();
   if (listAdapter == null)
     return;
   int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
   int totalHeight = 0;
   View view = null;
   for (int i = 0; i < listAdapter.getCount(); i = i+2) {
     view = listAdapter.getView(i, view, listView);
     view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
     totalHeight += view.getMeasuredHeight();
   }
   ViewGroup.LayoutParams params = listView.getLayoutParams();
   params.height = totalHeight;
   listView.setLayoutParams(params);
   listView.requestLayout();
 }

代码示例来源:origin: sealtalk/sealtalk-android

public void setGridViewHeight(GridView gridView) {
  if (gridView == null) {
    return;
  }
  ViewGroup.LayoutParams params = gridView.getLayoutParams();
  //获取ActionBar高度
  TypedValue tv = new TypedValue();
  int actionBarHeight = 0;
  if (mContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
    actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, mContext.getResources().getDisplayMetrics());
  }
  /**
   * 获取状态栏高度
   * */
  int statusBarHeight = 0;
  if (getResources() != null) {
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
      //根据资源ID获取响应的尺寸值
      statusBarHeight = getResources().getDimensionPixelSize(resourceId);
    }
  }
  params.height = getScreenHeight() - getSolidHeight() - actionBarHeight - statusBarHeight;
  // 设置参数
  gridView.setLayoutParams(params);
}

代码示例来源:origin: stackoverflow.com

GridView grid = new GridView(this);  
grid.setId(215236);  
grid.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));  
grid.setBackgroundColor(Color.WHITE);  
grid.setColumnWidth(GridView.AUTO_FIT);  
grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);

代码示例来源:origin: stackoverflow.com

GridView gridView;
int gridWidth = 1280;
int gridHeight = 500;
int gridItemWidth = 60;
int gridItemHeight = 50;
int gridRows = 3, gridColumns = 4;
int vSpacing = (gridHeight - (gridItemHeight * gridRows))/gridRows;
int hSpacing = (gridWidth - (gridItemWidth * gridColumns))/gridColumns;
gridView.setLayoutParams(new LayoutParams(gridWidth, gridHeight));  

gridView.setColumnWidth(gridItemWidth);
gridView.setNumColumns(gridColumns);

gridView.setHorizontalSpacing(hSpacing);
gridView.setVerticalSpacing(vSpacing);

代码示例来源:origin: stackoverflow.com

GridView gv = new GridView(context);
gv.setId(999999);
gv.setLayoutParams(new    
GridView.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
gv.setBackgroundColor(Color.GREEN);
gv.setNumColumns(3);
gv.setColumnWidth(GridView.AUTO_FIT);
gv.setVerticalSpacing(5);
gv.setHorizontalSpacing(5);
gv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gv.setGravity(Gravity.CENTER);

代码示例来源:origin: werbhelius/Werb

/**
 * 创建显示表情的GridView
 */
private GridView createEmotionGridView(List<String> emotionNames, int gvWidth, int padding, int itemWidth, int gvHeight) {
  // 创建GridView
  GridView gv = new GridView(context);
  gv.setBackgroundResource(R.color.bg_gray);
  gv.setSelector(R.color.transparent);
  gv.setNumColumns(7);
  gv.setPadding(padding, padding, padding, padding);
  gv.setHorizontalSpacing(padding);
  gv.setVerticalSpacing(padding);
  ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(gvWidth, gvHeight);
  gv.setLayoutParams(params);
  // 给GridView设置表情图片
  EmotionGvAdapter adapter = new EmotionGvAdapter(context, emotionNames, itemWidth);
  gv.setAdapter(adapter);
  gv.setOnItemClickListener(this);
  return gv;
}

代码示例来源:origin: stackoverflow.com

GridView grid = new GridView(this);
 grid.setId(ViewIdentification.getId());
 grid.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
 grid.setBackgroundColor(Color.WHITE);
 grid.setNumColumns(3);
 grid.setColumnWidth(GridView.AUTO_FIT);
 grid.setVerticalSpacing(5);
 grid.setHorizontalSpacing(5);
 grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);

代码示例来源:origin: bilibili/BiliShare

private void createShareWindowIfNeed() {
  if (mShareWindow != null)
    return;
  Context context = getContext();
  grid = createShareGridView(context, getItemClickListener());
  RelativeLayout.LayoutParams gridParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  gridParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
  grid.setLayoutParams(gridParams);
  mContainerView = new RelativeLayout(getContext());
  mContainerView.setBackgroundColor(getContext().getResources().getColor(R.color.bili_socialize_black_trans));
  RelativeLayout.LayoutParams containerParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
  mContainerView.setLayoutParams(containerParams);
  mContainerView.addView(grid);
  mContainerView.setOnClickListener(this);
  mShareWindow = new PopupWindow(mContainerView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
  grid.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
  mShareWindow.setOutsideTouchable(true);
  mShareWindow.setAnimationStyle(-1);
  mShareWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
    @Override
    public void onDismiss() {
      if (getDismissListener() != null)
        getDismissListener().onDismiss();
    }
  });
}

代码示例来源:origin: AppLozic/Applozic-Android-SDK

public void createView() {
  removeAllViews();
  GridView gridView = new GridView(getContext());
  LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  gridView.setLayoutParams(params);
  gridView.setNumColumns(3);
  gridView.setHorizontalSpacing(1);
  gridView.setVerticalSpacing(1);
  gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
  prepareAttachmentData(gridView);
  gridView.setOnItemClickListener(this);
  addView(gridView);
}

代码示例来源:origin: shinezejian/emotionkeyboard

/**
 * 创建显示表情的GridView
 */
private GridView createEmotionGridView(List<String> emotionNames, int gvWidth, int padding, int itemWidth, int gvHeight) {
  // 创建GridView
  GridView gv = new GridView(getActivity());
  //设置点击背景透明
  gv.setSelector(android.R.color.transparent);
  //设置7列
  gv.setNumColumns(7);
  gv.setPadding(padding, padding, padding, padding);
  gv.setHorizontalSpacing(padding);
  gv.setVerticalSpacing(padding * 2);
  //设置GridView的宽高
  ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(gvWidth, gvHeight);
  gv.setLayoutParams(params);
  // 给GridView设置表情图片
  EmotionGridViewAdapter adapter = new EmotionGridViewAdapter(getActivity(), emotionNames, itemWidth,emotion_map_type);
  gv.setAdapter(adapter);
  //设置全局点击事件
  gv.setOnItemClickListener(GlobalOnItemClickManagerUtils.getInstance(getActivity()).getOnItemClickListener(emotion_map_type));
  return gv;
}

相关文章

GridView类方法