android设置圆角按钮ButtonM
来源:互联网 发布:广告创意提案 知乎 编辑:程序博客网 时间:2024/06/16 19:37
定义ButtonM
package com.example.demo;import android.content.Context;import android.graphics.Color;import android.graphics.drawable.GradientDrawable;import android.util.AttributeSet;import android.view.Gravity;import android.view.MotionEvent;import android.view.View;import android.widget.Button;public class ButtonM extends Button {private GradientDrawable gradientDrawable;//控件的样式 private String backColors = "";//背景色,String类型 private int backColori = 0;//背景色,int类型 private String backColorSelecteds = "";//按下后的背景色,String类型 private int backColorSelectedi = 0;//按下后的背景色,int类型 private int backGroundImage = 0;//背景图,只提供了Id private int backGroundImageSeleted = 0;//按下后的背景图,只提供了Id private String textColors = "";//文字颜色,String类型 private int textColori = 0;//文字颜色,int类型 private String textColorSeleteds = "";//按下后的文字颜色,String类型 private int textColorSeletedi = 0;//按下后的文字颜色,int类型 private float radius = 8;//圆角半径 private int shape = 0;//圆角样式,矩形、圆形等,由于矩形的Id为0,默认为矩形 private Boolean fillet = false;//是否设置圆角 public ButtonM(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public ButtonM(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ButtonM(Context context) { this(context, null); } private void init() { //将Button的默认背景色改为透明,本人不喜欢原来的颜色 if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(Color.TRANSPARENT); }else { setBackgroundColor(Color.TRANSPARENT); } //设置文字默认居中 setGravity(Gravity.CENTER); //设置Touch事件 setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent event) { //按下改变样式 setColor(event.getAction()); //此处设置为false,防止Click事件被屏蔽 return false; } }); } //改变样式 private void setColor(int state){ if (state == MotionEvent.ACTION_DOWN) { //按下 if (backColorSelectedi != 0) { //先判断是否设置了按下后的背景色int型 if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(backColorSelectedi); }else { setBackgroundColor(backColorSelectedi); } }else if (!backColorSelecteds.equals("")) { if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(Color.parseColor(backColorSelecteds)); }else { setBackgroundColor(Color.parseColor(backColorSelecteds)); } } //判断是否设置了按下后文字的颜色 if (textColorSeletedi != 0) { setTextColor(textColorSeletedi); }else if (!textColorSeleteds.equals("")) { setTextColor(Color.parseColor(textColorSeleteds)); } //判断是否设置了按下后的背景图 if (backGroundImageSeleted != 0) { setBackgroundResource(backGroundImageSeleted); } } if (state == MotionEvent.ACTION_UP) { //抬起 if (backColori == 0 && backColors.equals("")) { //如果没有设置背景色,默认改为透明 if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(Color.TRANSPARENT); }else { setBackgroundColor(Color.TRANSPARENT); } }else if(backColori != 0){ if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(backColori); }else { setBackgroundColor(backColori); } }else { if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(Color.parseColor(backColors)); }else { setBackgroundColor(Color.parseColor(backColors)); } } //如果为设置字体颜色,默认为黑色 if (textColori == 0 && textColors.equals("")) { setTextColor(Color.BLACK); }else if (textColori != 0) { setTextColor(textColori); }else { setTextColor(Color.parseColor(textColors)); } if (backGroundImage != 0) { setBackgroundResource(backGroundImage); } } } /** * 设置按钮的背景色,如果未设置则默认为透明 * @param backColor */ public void setBackColor(String backColor) { this.backColors = backColor; if (backColor.equals("")) { if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(Color.TRANSPARENT); }else { setBackgroundColor(Color.TRANSPARENT); } }else { if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(Color.parseColor(backColor)); }else { setBackgroundColor(Color.parseColor(backColor)); } } } /** * 设置按钮的背景色,如果未设置则默认为透明 * @param backColor */ public void setBackColor(int backColor) { this.backColori = backColor; if (backColori == 0) { if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(Color.TRANSPARENT); }else { setBackgroundColor(Color.TRANSPARENT); } }else { if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setColor(backColor); }else { setBackgroundColor(backColor); } } } /** * 设置按钮按下后的颜色 * @param backColorSelected */ public void setBackColorSelected(int backColorSelected) { this.backColorSelectedi = backColorSelected; } /** * 设置按钮按下后的颜色 * @param backColorSelected */ public void setBackColorSelected(String backColorSelected) { this.backColorSelecteds = backColorSelected; } /** * 设置按钮的背景图 * @param backGroundImage */ public void setBackGroundImage(int backGroundImage) { this.backGroundImage = backGroundImage; if (backGroundImage != 0) { setBackgroundResource(backGroundImage); } } /** * 设置按钮按下的背景图 * @param backGroundImageSeleted */ public void setBackGroundImageSeleted(int backGroundImageSeleted) { this.backGroundImageSeleted = backGroundImageSeleted; } /** * 设置按钮圆角半径大小 * @param radius */ public void setRadius(float radius) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } gradientDrawable.setCornerRadius(radius); } /** * 设置按钮文字颜色 * @param textColor */ public void setTextColors(String textColor) { this.textColors = textColor; setTextColor(Color.parseColor(textColor)); } /** * 设置按钮文字颜色 * @param textColor */ public void setTextColori(int textColor) { this.textColori = textColor; setTextColor(textColor); } /** * 设置按钮按下的文字颜色 * @param textColor */ public void setTextColorSelected(String textColor) { this.textColorSeleteds = textColor; } /** * 设置按钮按下的文字颜色 * @param textColor */ public void setTextColorSelected(int textColor) { this.textColorSeletedi = textColor; } /** * 按钮的形状 * @param shape */ public void setShape(int shape) { this.shape = shape; } /** * 设置其是否为圆角 * @param fillet */ @SuppressWarnings("deprecation") public void setFillet(Boolean fillet) { this.fillet = fillet; if (fillet) { if (gradientDrawable == null) { gradientDrawable = new GradientDrawable(); } //GradientDrawable.RECTANGLE gradientDrawable.setShape(shape); gradientDrawable.setCornerRadius(radius); setBackgroundDrawable(gradientDrawable); } }}
随机生成颜色:
package com.example.demo;import java.util.Random;public class ColorUtils {/** * 生成随机颜色代码 * * @return */public static synchronized String getRandomColorCode() {// 颜色代码位数int colorLength = 6;// 颜色代码数组char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2','3', '4', '5', '6', '7', '8', '9' };StringBuffer sb = new StringBuffer("#");//StringBuffer sb = new StringBuffer();Random random = new Random();for (int i = 0; i < colorLength; i++) {sb.append(codeSequence[random.nextInt(16)]);}return sb.toString();}/** * 获取十六进制的颜色代码.例如 "#6E36B4" , For HTML , * * @return String */public static String getRandColorCode() {String r, g, b;Random random = new Random();r = Integer.toHexString(random.nextInt(256)).toUpperCase();g = Integer.toHexString(random.nextInt(256)).toUpperCase();b = Integer.toHexString(random.nextInt(256)).toUpperCase();r = r.length() == 1 ? "0" + r : r;g = g.length() == 1 ? "0" + g : g;b = b.length() == 1 ? "0" + b : b;return r + g + b;}}主Activity
package com.example.demo;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.graphics.drawable.GradientDrawable;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.LinearLayout;import android.widget.Toast;public class MainActivity extends Activity {// 定义三个空布局用来装载Button控件,只为演示效果,实际开发中不推荐使用//private LinearLayout llButtonM1;//private LinearLayout llButtonM2;private LinearLayout llButtonM3;private HorizontalListView view;HorizontalAdapter adapter;List<Same> list; // 定义三个ButtonM数组//private ButtonM[] buttonM1;//private ButtonM[] buttonM2;private ButtonM[] buttonM3;private ButtonM buttonM;// 定义两组颜色值,按下与未按下的按钮背景色//private static final String[] colorList = { ColorUtils.getRandomColorCode(), ColorUtils.getRandomColorCode(),//ColorUtils.getRandomColorCode(), ColorUtils.getRandomColorCode() };//private static final String[] colorSelectedList = { "#3C3779", "#88354C",//"#613E70", "#00677D" };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();list = new ArrayList<Same>();for(int i = 0; i < 6; i++){Same same = new Same();same.setName("10"+i);list.add(same);}adapter = new HorizontalAdapter(list, this);view.setAdapter(adapter);}// 初始化控件private void initView() {// 实例化布局控件//llButtonM1 = (LinearLayout) findViewById(R.id.ll_button1);//llButtonM2 = (LinearLayout) findViewById(R.id.ll_button2);llButtonM3 = (LinearLayout) findViewById(R.id.ll_button3);view = (HorizontalListView) findViewById(R.id.horizontallistView);buttonM = (ButtonM) findViewById(R.id.btn2);// 实例化控件数组,各定义4个//buttonM1 = new ButtonM[4];//buttonM2 = new ButtonM[4];buttonM3 = new ButtonM[4];// 获取屏幕的宽度,每行四个Button,间隙为60共300,除4为每个控件的宽度@SuppressWarnings("deprecation")int btnWidth = (getWindowManager().getDefaultDisplay().getWidth() - 300) / 4;LinearLayout.LayoutParams lp1;// 为buttonM3设置样式,圆形//buttonM = new ButtonM(this);buttonM.setTextColori(android.graphics.Color.WHITE);buttonM.setTextSize(14);// 设置为圆形,默认为矩形,GradientDrawable.RECTANGLEbuttonM.setShape(GradientDrawable.OVAL);buttonM.setFillet(true);buttonM.setBackColor(ColorUtils.getRandomColorCode());buttonM.setText("TEXT");// 定义buttonM1的布局,宽度自适应,高度为宽度的0.6倍,权重为1// 也可以写成lp1 = new LinearLayout.LayoutParams(btnWidth,(int) (btnWidth// * 0.6));lp1 = new LinearLayout.LayoutParams(btnWidth,btnWidth);// 控件距离其右侧控件的距离,此处为60lp1.setMargins(60, 0, 60, 0);buttonM.setLayoutParams(lp1);//llButtonM1.setPadding(60, 0, 0, 0);//llButtonM1.addView(buttonM);//// 定义第一个布局//LinearLayout.LayoutParams lp1;//for (int i = 0; i < 4; i++) {//// 为buttonM1设置样式,直角矩形//buttonM1[i] = new ButtonM(this);//// 字体颜色//buttonM1[i].setTextColori(android.graphics.Color.WHITE);//// 字体大小//buttonM1[i].setTextSize(14);//// 背景色//buttonM1[i].setBackColor(Color.parseColor(colorList[i]));//// 选中的背景色//buttonM1[i].setBackColorSelected(Color//.parseColor(colorSelectedList[i]));//// 文字提示//buttonM1[i].setText("TEXT" + i);//// 此处设置Id的值为i,否则onClick中v.getId()将永远为-1//buttonM1[i].setId(i);//// 定义buttonM1的布局,宽度自适应,高度为宽度的0.6倍,权重为1//// 也可以写成lp1 = new LinearLayout.LayoutParams(btnWidth,(int) (btnWidth//// * 0.6));//lp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,//(int) (btnWidth * 0.6), 1.0f);//// 控件距离其右侧控件的距离,此处为60//lp1.setMargins(0, 0, 60, 0);//buttonM1[i].setLayoutParams(lp1);//// 设置buttonM1的点击事件//buttonM1[i].setOnClickListener(new OnClickListener() {//@Override//public void onClick(View v) {//Toast.makeText(MainActivity.this,//"您选择了第" + v.getId() + "个", Toast.LENGTH_SHORT)//.show();//}//});//// 设置PaddingLeft为60//llButtonM1.setPadding(60, 0, 0, 0);//// 将buttonM1添加到llButtonM1中//llButtonM1.addView(buttonM1[i]);//}//// 定义第二个布局//LinearLayout.LayoutParams lp2;//for (int i = 0; i < 4; i++) {//// 为buttonM2设置样式,圆角矩形//buttonM2[i] = new ButtonM(this);//buttonM2[i].setTextColori(android.graphics.Color.WHITE);//buttonM2[i].setTextSize(14);//// 设置是否为圆角//buttonM2[i].setFillet(true);//// 设置圆角的半径大小//buttonM2[i].setRadius(18);//buttonM2[i].setBackColor(Color.parseColor(colorList[i]));//buttonM2[i].setBackColorSelected(Color//.parseColor(colorSelectedList[i]));//buttonM2[i].setText("TEXT" + i);//buttonM2[i].setId(i);//lp2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,//(int) (btnWidth * 0.6), 1.0f);//lp2.setMargins(0, 0, 60, 0);//buttonM2[i].setLayoutParams(lp2);//buttonM2[i].setOnClickListener(new OnClickListener() {//@Override//public void onClick(View v) {//Toast.makeText(MainActivity.this,//"您选择了第" + v.getId() + "个", Toast.LENGTH_SHORT)//.show();//}//});//llButtonM2.setPadding(60, 0, 0, 0);//llButtonM2.addView(buttonM2[i]);//}// 定义第三个布局LinearLayout.LayoutParams lp3;for (int i = 0; i < 4; i++) {// 为buttonM3设置样式,圆形buttonM3[i] = new ButtonM(this);buttonM3[i].setTextColori(android.graphics.Color.WHITE);buttonM3[i].setTextSize(14);// 设置为圆形,默认为矩形,GradientDrawable.RECTANGLEbuttonM3[i].setShape(GradientDrawable.OVAL);buttonM3[i].setFillet(true);buttonM3[i].setBackColor(ColorUtils.getRandomColorCode());buttonM3[i].setBackColorSelected(ColorUtils.getRandomColorCode());buttonM3[i].setText("TEXT" + (i + 1));buttonM3[i].setId(i+1);lp3 = new LinearLayout.LayoutParams(btnWidth, btnWidth);lp3.setMargins(0, 0, 60, 0);buttonM3[i].setLayoutParams(lp3);buttonM3[i].setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this,"您选择了第" + v.getId() + "个", Toast.LENGTH_SHORT).show();}});llButtonM3.setPadding(60, 0, 0, 0);llButtonM3.addView(buttonM3[i]);}}}水平滚动listView
package com.example.demo;import java.util.LinkedList;import java.util.Queue;import android.content.Context;import android.database.DataSetObserver;import android.graphics.Rect;import android.util.AttributeSet;import android.view.GestureDetector;import android.view.GestureDetector.OnGestureListener;import android.view.MotionEvent;import android.view.View;import android.widget.AdapterView;import android.widget.ListAdapter;import android.widget.Scroller;public class HorizontalListView extends AdapterView<ListAdapter> { public boolean mAlwaysOverrideTouch = true; protected ListAdapter mAdapter; private int mLeftViewIndex = -1; private int mRightViewIndex = 0; protected int mCurrentX; protected int mNextX; private int mMaxX = Integer.MAX_VALUE; private int mDisplayOffset = 0; protected Scroller mScroller; private GestureDetector mGesture; private Queue<View> mRemovedViewQueue = new LinkedList<View>(); private OnItemSelectedListener mOnItemSelected; private OnItemClickListener mOnItemClicked; private OnItemLongClickListener mOnItemLongClicked; private boolean mDataChanged = false; public HorizontalListView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } private synchronized void initView() { mLeftViewIndex = -1; mRightViewIndex = 0; mDisplayOffset = 0; mCurrentX = 0; mNextX = 0; mMaxX = Integer.MAX_VALUE; mScroller = new Scroller(getContext()); mGesture = new GestureDetector(getContext(), mOnGesture); } @Override public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener) { mOnItemSelected = listener; } @Override public void setOnItemClickListener(AdapterView.OnItemClickListener listener){ mOnItemClicked = listener; } @Override public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener) { mOnItemLongClicked = listener; } private DataSetObserver mDataObserver = new DataSetObserver() { @Override public void onChanged() { synchronized(HorizontalListView.this){ mDataChanged = true; } invalidate(); requestLayout(); } @Override public void onInvalidated() { reset(); invalidate(); requestLayout(); } }; @Override public ListAdapter getAdapter() { return mAdapter; } @Override public View getSelectedView() { //TODO: implement return null; } @Override public void setAdapter(ListAdapter adapter) { if(mAdapter != null) { mAdapter.unregisterDataSetObserver(mDataObserver); } mAdapter = adapter; mAdapter.registerDataSetObserver(mDataObserver); reset(); } private synchronized void reset(){ initView(); removeAllViewsInLayout(); requestLayout(); } @Override public void setSelection(int position) { //TODO: implement } private void addAndMeasureChild(final View child, int viewPos) { LayoutParams params = child.getLayoutParams(); if(params == null) { params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); } addViewInLayout(child, viewPos, params, true); child.measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST)); } @Override protected synchronized void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); if(mAdapter == null){ return; } if(mDataChanged){ int oldCurrentX = mCurrentX; initView(); removeAllViewsInLayout(); mNextX = oldCurrentX; mDataChanged = false; } if(mScroller.computeScrollOffset()){ int scrollx = mScroller.getCurrX(); mNextX = scrollx; } if(mNextX <= 0){ mNextX = 0; mScroller.forceFinished(true); } if(mNextX >= mMaxX) { mNextX = mMaxX; mScroller.forceFinished(true); } int dx = mCurrentX - mNextX; removeNonVisibleItems(dx); fillList(dx); positionItems(dx); mCurrentX = mNextX; if(!mScroller.isFinished()){ post(new Runnable(){ @Override public void run() { requestLayout(); } }); } } private void fillList(final int dx) { int edge = 0; View child = getChildAt(getChildCount()-1); if(child != null) { edge = child.getRight(); } fillListRight(edge, dx); edge = 0; child = getChildAt(0); if(child != null) { edge = child.getLeft(); } fillListLeft(edge, dx); } private void fillListRight(int rightEdge, final int dx) { while(rightEdge + dx < getWidth() && mRightViewIndex < mAdapter.getCount()) { View child = mAdapter.getView(mRightViewIndex, mRemovedViewQueue.poll(), this); addAndMeasureChild(child, -1); rightEdge += child.getMeasuredWidth(); if(mRightViewIndex == mAdapter.getCount()-1) { mMaxX = mCurrentX + rightEdge - getWidth(); } if (mMaxX < 0) { mMaxX = 0; } mRightViewIndex++; } } private void fillListLeft(int leftEdge, final int dx) { while(leftEdge + dx > 0 && mLeftViewIndex >= 0) { View child = mAdapter.getView(mLeftViewIndex, mRemovedViewQueue.poll(), this); addAndMeasureChild(child, 0); leftEdge -= child.getMeasuredWidth(); mLeftViewIndex--; mDisplayOffset -= child.getMeasuredWidth(); } } private void removeNonVisibleItems(final int dx) { View child = getChildAt(0); while(child != null && child.getRight() + dx <= 0) { mDisplayOffset += child.getMeasuredWidth(); mRemovedViewQueue.offer(child); removeViewInLayout(child); mLeftViewIndex++; child = getChildAt(0); } child = getChildAt(getChildCount()-1); while(child != null && child.getLeft() + dx >= getWidth()) { mRemovedViewQueue.offer(child); removeViewInLayout(child); mRightViewIndex--; child = getChildAt(getChildCount()-1); } } private void positionItems(final int dx) { if(getChildCount() > 0){ mDisplayOffset += dx; int left = mDisplayOffset; for(int i=0;i<getChildCount();i++){ View child = getChildAt(i); int childWidth = child.getMeasuredWidth(); child.layout(left, 0, left + childWidth, child.getMeasuredHeight()); left += childWidth + child.getPaddingRight(); } } } public synchronized void scrollTo(int x) { mScroller.startScroll(mNextX, 0, x - mNextX, 0); requestLayout(); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { boolean handled = super.dispatchTouchEvent(ev); handled |= mGesture.onTouchEvent(ev); return handled; } protected boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { synchronized(HorizontalListView.this){ mScroller.fling(mNextX, 0, (int)-velocityX, 0, 0, mMaxX, 0, 0); } requestLayout(); return true; } protected boolean onDown(MotionEvent e) { mScroller.forceFinished(true); return true; } private OnGestureListener mOnGesture = new GestureDetector.SimpleOnGestureListener() { @Override public boolean onDown(MotionEvent e) { return HorizontalListView.this.onDown(e); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return HorizontalListView.this.onFling(e1, e2, velocityX, velocityY); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { synchronized(HorizontalListView.this){ mNextX += (int)distanceX; } requestLayout(); return true; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { for(int i=0;i<getChildCount();i++){ View child = getChildAt(i); if (isEventWithinView(e, child)) { if(mOnItemClicked != null){ mOnItemClicked.onItemClick(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId( mLeftViewIndex + 1 + i )); } if(mOnItemSelected != null){ mOnItemSelected.onItemSelected(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId( mLeftViewIndex + 1 + i )); } break; } } return true; } @Override public void onLongPress(MotionEvent e) { int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); if (isEventWithinView(e, child)) { if (mOnItemLongClicked != null) { mOnItemLongClicked.onItemLongClick(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId(mLeftViewIndex + 1 + i)); } break; } } } private boolean isEventWithinView(MotionEvent e, View child) { Rect viewRect = new Rect(); int[] childPosition = new int[2]; child.getLocationOnScreen(childPosition); int left = childPosition[0]; int right = left + child.getWidth(); int top = childPosition[1]; int bottom = top + child.getHeight(); viewRect.set(left, top, right, bottom); return viewRect.contains((int) e.getRawX(), (int) e.getRawY()); } }; }listView的Adapter
package com.example.demo;import java.util.List;import android.app.Activity;import android.content.Context;import android.graphics.drawable.GradientDrawable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.LinearLayout;public class HorizontalAdapter extends BaseAdapter {private List<Same> sames;private Activity mContext;public HorizontalAdapter(List<Same> sames, Activity mContext) {super();this.sames = sames;this.mContext = mContext;}@Overridepublic int getCount() {return sames.size();}@Overridepublic Object getItem(int arg0) {return sames.get(arg0);}@Overridepublic long getItemId(int arg0) {return arg0;}@Overridepublic View getView(int arg0, View arg1, ViewGroup arg2) {ViewHolder holder;if (arg1 == null) {holder = new ViewHolder();arg1 = LayoutInflater.from(mContext).inflate(R.layout.activity, null);holder.ll = (LinearLayout) arg1.findViewById(R.id.ll);holder.btn = (ButtonM) arg1.findViewById(R.id.btn);arg1.setTag(holder);}else{holder = (ViewHolder) arg1.getTag();}LinearLayout.LayoutParams lp1;// 为buttonM3设置样式,圆形holder.btn.setTextColori(android.graphics.Color.WHITE);holder.btn.setTextSize(10);// 设置为圆形,默认为矩形,GradientDrawable.RECTANGLEholder.btn.setShape(GradientDrawable.OVAL);holder.btn.setFillet(true);holder.btn.setBackColor(ColorUtils.getRandomColorCode());holder.btn.setText("校长室");lp1 = new LinearLayout.LayoutParams(dip2px(mContext, 40), dip2px(mContext, 40));// 控件距离其右侧控件的距离,此处为20lp1.setMargins(0, 0, 20, 0);holder.btn.setLayoutParams(lp1);return arg1;}class ViewHolder{LinearLayout ll;ButtonM btn;}/** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } }list<Same>
package com.example.demo;public class Same {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}}activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/ll_button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:orientation="horizontal" > <com.example.demo.ButtonM android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:id="@+id/ll_button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:orientation="horizontal" > </LinearLayout> <LinearLayout android:id="@+id/ll_button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:orientation="horizontal" > </LinearLayout> <com.example.demo.HorizontalListView android:id="@+id/horizontallistView" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="20dp" android:background="@android:color/white" android:cacheColorHint="#00000000" /></LinearLayout>activity
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <com.example.demo.ButtonM android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content"/></LinearLayout>
1 0
- android设置圆角按钮ButtonM
- Android如何设置圆角按钮
- android基础6——设置圆角按钮
- android 客户端学习-给button按钮设置圆角
- Android如何设置圆角按钮 类似微信的登陆按钮
- 1. android 设置圆角按钮后,按下按钮后,还能改变按钮的颜色
- Android 设置透明按钮
- storyboard设置圆角按钮
- android圆角按钮
- android 圆角按钮
- Android 圆角按钮
- Android按钮设置文字变色
- Android 按钮设置边框实例
- Android 多按钮监听器设置
- Android设置按钮为透明
- Android设置按钮不可点击
- android设置按钮背景透明
- 设置圆角按钮的方法
- php使用openssl来实现非对称加密
- 【线性规划与网络流24题 24】骑士共存问题
- [李景山php]每天laravel-20161129|BelongsToMany.php-1
- 职场人士如何减压-如何缓解职场强迫症
- [Medium]Single Number III
- android设置圆角按钮ButtonM
- android-pdf-viewer在android studio应用问题说明
- 理解java的I/O流:
- 反射执行带泛型的方法
- TextBox TextMode="Password" 不能赋值,页面回滚时数据会丢失的解决方法
- thinking in uml 读书笔记 [第二章 建模基础]
- listview和scrollview嵌套使用--解决方案
- Hadoop配置与调试
- Java进程基础