下面列出了android.widget.GridView#setStretchMode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
@Override public void showThemeChooseDialog() {
AlertDialog.Builder builder = DialogUtils.makeDialogBuilder(activity);
builder.setTitle(R.string.change_theme);
Integer[] res = new Integer[] { R.drawable.deep_purple_round,
R.drawable.brown_round, R.drawable.blue_round,
R.drawable.blue_grey_round, R.drawable.yellow_round,
R.drawable.red_round, R.drawable.pink_round,
R.drawable.green_round };
List<Integer> list = Arrays.asList(res);
ColorsListAdapter adapter = new ColorsListAdapter(getActivity(), list);
adapter.setCheckItem(
ThemeUtils.getCurrentTheme(activity).getIntValue());
GridView gridView = (GridView) LayoutInflater.from(activity)
.inflate(
R.layout.colors_panel_layout,
null);
gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gridView.setCacheColorHint(0);
gridView.setAdapter(adapter);
builder.setView(gridView);
final AlertDialog dialog = builder.show();
gridView.setOnItemClickListener((parent, view, position, id) -> {
dialog.dismiss();
mSettingPresenter.onThemeChoose(position);
});
}
@Override
public void applyStylesWithContext(PXStylerContext stylerContext) {
GridStyle style = stylerContext.getGridStyle();
if (style != null) {
GridView view = (GridView) stylerContext.getStyleable();
if (style.columnCount != Integer.MIN_VALUE) {
view.setNumColumns(style.columnCount);
}
if (style.columnWidth != Integer.MIN_VALUE) {
view.setColumnWidth(style.columnWidth);
}
if (style.columnGap != Integer.MIN_VALUE) {
view.setHorizontalSpacing(style.columnGap);
}
if (style.rowGap != Integer.MIN_VALUE) {
view.setVerticalSpacing(style.rowGap);
}
if (style.columnStretchMode != Integer.MIN_VALUE) {
view.setStretchMode(style.columnStretchMode);
}
}
super.applyStylesWithContext(stylerContext);
}
private void showThemeChooseDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(ws_Main3Activity.this);
builder.setTitle("设置主题");
Integer[] res = new Integer[]{R.drawable.red_round, R.drawable.brown_round, R.drawable.blue_round, R.drawable.blue_grey_round, R.drawable.yellow_round, R.drawable.deep_purple_round, R.drawable.pink_round, R.drawable.green_round};
List<Integer> list = Arrays.asList(res);
ColorsListAdapter adapter = new ColorsListAdapter(ws_Main3Activity.this, list);
adapter.setCheckItem(MyThemeUtils.getCurrentTheme(ws_Main3Activity.this).getIntValue());
GridView gridView = (GridView) LayoutInflater.from(ws_Main3Activity.this).inflate(R.layout.colors_panel_layout, null);
gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gridView.setCacheColorHint(0);
gridView.setAdapter(adapter);
builder.setView(gridView);
final AlertDialog dialog = builder.show();
gridView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
dialog.dismiss();
int value = MyThemeUtils.getCurrentTheme(ws_Main3Activity.this).getIntValue();
if (value != position) {
PreferenceUtils.getInstance(ws_Main3Activity.this).saveParam("change_theme_key", position);
MyThemeUtils.Theme.mapValueToTheme(position);
}
}
}
);
}
public EmoticonPageView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_emoticonpage, this);
mGvEmotion = (GridView) view.findViewById(R.id.gv_emotion);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
mGvEmotion.setMotionEventSplittingEnabled(false);
}
mGvEmotion.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
mGvEmotion.setCacheColorHint(0);
mGvEmotion.setSelector(new ColorDrawable(Color.TRANSPARENT));
mGvEmotion.setVerticalScrollBarEnabled(false);
}
@Override
public void horizontal_layout(GridView gridView) {
int spacingH = SystemUtil.dipToPx(10);
int itemWidth = SystemUtil.dipToPx(45);
int size = list.size() + 1;
int allWidth = ((spacingH + itemWidth) * size);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
allWidth, LinearLayout.LayoutParams.FILL_PARENT);
gridView.setLayoutParams(params);
gridView.setColumnWidth(itemWidth);
gridView.setHorizontalSpacing(spacingH);
gridView.setStretchMode(GridView.NO_STRETCH);
gridView.setNumColumns(size);
}
public EmoticonPageView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_emoticonpage, this);
mGvEmotion = (GridView) view.findViewById(R.id.gv_emotion);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
mGvEmotion.setMotionEventSplittingEnabled(false);
}
mGvEmotion.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
mGvEmotion.setCacheColorHint(0);
mGvEmotion.setSelector(new ColorDrawable(Color.TRANSPARENT));
mGvEmotion.setVerticalScrollBarEnabled(false);
}
private void addGridView() {
gridView = new GridView(this.getActivity());
gridView.setNumColumns(7);
gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gridView.setGravity(Gravity.CENTER_VERTICAL);
gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
gridView.setHorizontalScrollBarEnabled(false);
gridView.setVerticalScrollBarEnabled(false);
// gridView.setVerticalSpacing(1);
// gridView.setHorizontalSpacing(1);
gridView.setOnTouchListener(new View.OnTouchListener() {
// 将gridview中的触摸事件回传给gestureDetector
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// 点击任何一个item,得到这个item的日期(排除点击的是周日到周六(点击不响应))
int startPosition = calV.getStartPositon();
int endPosition = calV.getEndPosition();
if (startPosition <= position + 7 && position <= endPosition - 7) {
Day clickDay = calV.getItem(position);
Logger.d(clickDay.toString(), 2);
setTodayLunarInfo(clickDay);
// Toast.makeText(CalendarActivity.this, clickDay.toString(), Toast.LENGTH_SHORT).show();
}
}
});
gridView.setLayoutParams(new LinearLayout.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
}
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);
}
@Override
public View style(View view, JSONObject attributes) throws Exception {
super.style(view, attributes);
GridView gridView = (GridView) view;
if(attributes.has("numColumns")) {
gridView.setNumColumns(attributes.getInt("numColumns"));
}
if(attributes.has("selection")) {
gridView.setSelection(attributes.getInt("selection"));
}
if(attributes.has("horizontalSpacing")) {
gridView.setHorizontalSpacing(Display.unitToPx(attributes.getString("horizontalSpacing"), context));
}
if(attributes.has("verticalSpacing")) {
gridView.setVerticalSpacing(Display.unitToPx(attributes.getString("verticalSpacing"), context));
}
if(attributes.has("columnWidth")) {
gridView.setColumnWidth(Display.unitToPx(attributes.getString("columnWidth"), context));
}
if(attributes.has("stretchMode")) {
String mode = attributes.getString("stretchMode");
if(mode.equalsIgnoreCase("no_stretch")) {
gridView.setStretchMode(NO_STRETCH);
}else if(mode.equalsIgnoreCase("stretch_spacing")) {
gridView.setStretchMode(STRETCH_SPACING);
}else if(mode.equalsIgnoreCase("stretch_column_width")) {
gridView.setStretchMode(STRETCH_COLUMN_WIDTH);
}else if(mode.equalsIgnoreCase("stretch_spacing_uniform")) {
gridView.setStretchMode(STRETCH_SPACING_UNIFORM);
}
}
if(attributes.has("gravity")) {
String gravity = attributes.getString("gravity");
if(gravity.equalsIgnoreCase("start")) {
gridView.setGravity(Gravity.START);
}else if(gravity.equalsIgnoreCase("top")) {
gridView.setGravity(Gravity.TOP);
}else if(gravity.equalsIgnoreCase("end")) {
gridView.setGravity(Gravity.END);
}else if(gravity.equalsIgnoreCase("bottom")) {
gridView.setGravity(Gravity.BOTTOM);
}else if(gravity.equalsIgnoreCase("center")) {
gridView.setGravity(Gravity.CENTER);
}else if(gravity.equalsIgnoreCase("center_horizontal")) {
gridView.setGravity(Gravity.CENTER_HORIZONTAL);
}else if(gravity.equalsIgnoreCase("center_vertical")) {
gridView.setGravity(Gravity.CENTER_VERTICAL);
}
}
return gridView;
}
public TabMenu(Context context,OnItemClickListener titleClick,OnItemClickListener bodyClick,
MenuTitleAdapter titleAdapter,int colorBgTabMenu,int aniTabMenu)
{
super(context);
mLayout = new LinearLayout(context);
mLayout.setOrientation(LinearLayout.VERTICAL);
//标题选项栏
gvTitle = new GridView(context);
gvTitle.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
gvTitle.setNumColumns(titleAdapter.getCount());
gvTitle.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gvTitle.setVerticalSpacing(1);
gvTitle.setHorizontalSpacing(1);
gvTitle.setGravity(Gravity.CENTER);
gvTitle.setOnItemClickListener(titleClick);
gvTitle.setAdapter(titleAdapter);
gvTitle.setSelector(new ColorDrawable(Color.TRANSPARENT));//选中的时候为透明色
this.titleAdapter=titleAdapter;
//子选项栏
gvBody = new GridView(context);
gvBody.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
gvBody.setSelector(new ColorDrawable(Color.TRANSPARENT));//选中的时候为透明色
gvBody.setNumColumns(4);
gvBody.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gvBody.setVerticalSpacing(10);
gvBody.setHorizontalSpacing(10);
gvBody.setPadding(5, 5, 5, 5);
gvBody.setGravity(Gravity.CENTER);
gvBody.setOnItemClickListener(bodyClick);
mLayout.addView(gvTitle);
mLayout.addView(gvBody);
//设置默认项
this.setContentView(mLayout);
this.setWidth(LayoutParams.FILL_PARENT);
this.setHeight(LayoutParams.WRAP_CONTENT);
this.setBackgroundDrawable(new ColorDrawable(colorBgTabMenu));// 设置TabMenu菜单背景
this.setAnimationStyle(aniTabMenu);
this.setFocusable(true);// menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应
}
/**
* 设置笑脸被点击后的表情数据
*/
private void setSmilingfaceData() {
mV_myScrollView = new MyScrollView(this);
mList_emoji = FaceConversionUtil.getInstace().emojiLists;
// 添加表情页
mList_emojiAdapter = new ArrayList<EmojiAdapter>();
mV_myScrollView.removeAllViews();
for (int i = 0; i < mList_emoji.size(); i++) {
// GridView的一些特殊属性:
//
// 1.android:numColumns=”auto_fit” //GridView的列数设置为自动
// 2.android:columnWidth=”90dp " //每列的宽度,也就是Item的宽度
// 3.android:stretchMode=”columnWidth"//缩放与列宽大小同步
// 4.android:verticalSpacing=”10dp” //两行之间的边距
// 5.android:horizontalSpacing=”10dp” //两列之间的边距
// 6.android:cacheColorHint="#00000000" //去除拖动时默认的黑色背景
// 7.android:listSelector="#00000000" //去除选中时的黄色底色
// 8.android:scrollbars="none" //隐藏GridView的滚动条
// 9.android:fadeScrollbars="true" //设置为true就可以实现滚动条的自动隐藏和显示
// 10.android:fastScrollEnabled="true" //GridView出现快速滚动的按钮(至少滚动4页才会显示)
// 11.android:fadingEdge="none" //GridView衰落(褪去)边缘颜色为空,缺省值是vertical。(可以理解为上下边缘的提示色)
// 12.android:fadingEdgeLength="10dip" //定义的衰落(褪去)边缘的长度
// 13.android:stackFromBottom="true" //设置为true时,你做好的列表就会显示你列表的最下面
// 14.android:transcriptMode="alwaysScroll" //当你动态添加数据时,列表将自动往下滚动最新的条目可以自动滚动到可视范围内
// 15.android:drawSelectorOnTop="false" //点击某条记录不放,颜色会在记录的后面成为背景色,内容的文字可见(缺省为false)
//
GridView view = new GridView(this);
EmojiAdapter adapter = new EmojiAdapter(this, mList_emoji.get(i));
view.setAdapter(adapter);
mList_emojiAdapter.add(adapter);
view.setOnItemClickListener(this);
view.setNumColumns(7);
view.setBackgroundColor(Color.TRANSPARENT);
// view.setHorizontalSpacing(1); //两列之间的边距
// view.setVerticalSpacing(10);//两行之间的边距
view.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);//缩放与列宽大小同步
view.setCacheColorHint(0);//去除拖动时默认的黑色背景
// view.setPadding(5, 5, 5, 5);
view.setSelector(new ColorDrawable(Color.TRANSPARENT));
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
view.setLayoutParams(params);
view.setGravity(Gravity.CENTER);
mV_myScrollView.addView(view);
}
mLl_chat_smilingface_body.removeAllViews();
mLl_chat_smilingface_body.addView(mV_myScrollView);//将MyScrollView添加到内容显示区
RadioGroup.LayoutParams params_rb = new RadioGroup.LayoutParams(DensityUtil.dip2px(this, 8), DensityUtil.dip2px(this, 8));
int marginValue = DensityUtil.dip2px(this, 3);
params_rb.setMargins(marginValue, 0, marginValue, 0);
for (int i = 0; i < mV_myScrollView.getChildCount(); i++) {
RadioButton rbtn = new RadioButton(this);
rbtn.setButtonDrawable(R.drawable.cgt_selector_chat_radiobtn_bg);
rbtn.setId(i);
mRg_chat_smilingface_tab.addView(rbtn, params_rb);
if (i == 0) {
rbtn.setChecked(true);
}
}
/**
* 监听单选按钮是否被选中,
*/
mRg_chat_smilingface_tab.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
current = checkedId;
mV_myScrollView.moveToDest(checkedId);
}
});
/**
*
*/
mV_myScrollView.setChangedListener(new IPageChangedListener() {
@Override
public void changedTo(int pageId) {
current = pageId;
((RadioButton) mRg_chat_smilingface_tab.getChildAt(pageId)).setChecked(true);
}
});
}