Java Code Examples for android.widget.GridView#setGravity()

The following examples show how to use android.widget.GridView#setGravity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WeekCalendar.java    From WeekCalendar with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化日期
 */
private GridView addDayView() {
    LayoutParams params = new LayoutParams(
            AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
    final GridView gridView = new GridView(context);
    gridView.setNumColumns(7);
    gridView.setGravity(Gravity.CENTER_VERTICAL);
    gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
    gridView.setVerticalSpacing(1);
    gridView.setHorizontalSpacing(1);
    gridView.setLayoutParams(params);
    return gridView;
}
 
Example 2
Source File: MyColorPicker.java    From Ruisi with Apache License 2.0 5 votes vote down vote up
private void init() {
    String[] colors = mContext.getResources().getStringArray(R.array.color_list);
    for (int i = 0; i < colors.length; i++) {
        if (colorDatas == null) {
            colorDatas = new String[colors.length][2];
        }
        colorDatas[i][0] = colors[i].split(",")[0];
        colorDatas[i][1] = colors[i].split(",")[1];
    }

    gridView = new GridView(mContext);
    gridView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    gridView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
    gridView.setNumColumns(8);
    gridView.setPadding(DimenUtils.dip2px(mContext, 8), DimenUtils.dip2px(mContext, 12), DimenUtils.dip2px(mContext, 8), DimenUtils.dip2px(mContext, 12));
    gridView.setGravity(Gravity.CENTER);
    gridView.setHorizontalSpacing(DimenUtils.dip2px(mContext, 4));
    gridView.setVerticalSpacing(DimenUtils.dip2px(mContext, 12));
    gridView.setOnItemClickListener(this);

    setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
    setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    setBackgroundDrawable(ContextCompat.getDrawable(mContext, R.drawable.rec_solid_primary_bg));
    setFocusable(true);
    setContentView(gridView);

    adapter = new MyAdapter();
    gridView.setAdapter(adapter);
}
 
Example 3
Source File: CalendarFragment.java    From MaterialCalendar with Apache License 2.0 5 votes vote down vote up
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));
    }
 
Example 4
Source File: AlbumActivity.java    From socialauth-android with MIT License 5 votes vote down vote up
/**
 * collection of grid properties
 */
@SuppressWarnings("static-access")
public void getGridProperties(GridView view) {
	view.setNumColumns(3);
	view.setVerticalSpacing(5);
	view.setScrollBarStyle(view.SCROLLBARS_OUTSIDE_OVERLAY);
	view.setScrollingCacheEnabled(false);
	view.setGravity(Gravity.TOP);
	view.setSelector(new ColorDrawable(Color.parseColor("#00000000")));
	view.setClipChildren(true);
	view.setPadding(5, 5, 5, 5);
}
 
Example 5
Source File: GridViewStyler.java    From dynamico with Apache License 2.0 4 votes vote down vote up
@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;
}
 
Example 6
Source File: TabMenu.java    From coolreader with MIT License 4 votes vote down vote up
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菜单中的控件事件无法响应  
}
 
Example 7
Source File: ChatActivity.java    From weixin with Apache License 2.0 4 votes vote down vote up
/**
 * 设置笑脸被点击后的表情数据
 */
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);
		}
	});
}