//繼承LinearLayout public class MyResouView extends LinearLayout { public MyResouView(Context context) { this(context,null); } public MyResouView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public MyResouView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //設置縱向排列 this.setOrientation(VERTICAL); } }
(這裏我嘗試使用getMeasuredWidth()發現獲取不到值,或者獲取到的爲子控件寬度,使用getLayoutParams().width;照樣獲取不到,這裏緣由不清楚,(原諒我仍是個菜逼O(∩_∩)O哈哈~)getWidth()能夠獲取到控件寬度,可是必須得window獲取焦點後才能夠,因此我在給熱搜控件添加數據的方法中延時了100毫秒。)android
public void setResouwords(String[] resouwords) { mResouwords = resouwords; new Handler().postDelayed(new Runnable(){ public void run() { //獲取控件寬度 mMWidth = getWidth(); removeAllViews(); lineWidth=0; initView(); } }, 100); }
private void initView() { LinearLayout linearlayout; TextView textView; //建立每行LinearLayout的LayoutParams LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); //建立TextView的LayoutParams LinearLayout.LayoutParams paramsTextView=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); //設置每一個熱詞間距10 paramsTextView.setMargins(10,0,0,0); //設置每行間距2 params.topMargin=2; params.bottomMargin=2; linearlayout=new LinearLayout(getContext()); linearlayout.setLayoutParams(params); //設置每行LinearLayout排列方式爲橫向排列 linearlayout.setOrientation(HORIZONTAL); //當熱搜詞數據不爲空時循環添加TextView if(mResouwords.length>0){ for(int x=0;x<mResouwords.length;x++){ textView = new TextView(getContext()); textView.setLayoutParams(paramsTextView); textView.setText(mResouwords[x]); textView.setPadding(12, 6, 12, 6); textView.setTextSize(mTextSize); //計算每一個TextView的寬度 textView.measure(0,0); lineWidth+=textView.getMeasuredWidth()+10; //當寬度大於控件寬度的時候從新添加一行 if(lineWidth>mMWidth){ if(linearlayout!=null){ this.addView(linearlayout); } linearlayout=new LinearLayout(getContext()); linearlayout.setLayoutParams(params); lineWidth=0; } linearlayout.addView(textView); } //添加最後一行 this.addView(linearlayout); } }
首先在drawable中添加兩個shap文件(用來描繪邊框)數組
StateListDrawable//實現背景的切換 ColorStateList//實現文字顏色的切換
mBackgrounddrawable = new StateListDrawable(); GradientDrawable myGrad_unpress = (GradientDrawable) getResources().getDrawable(R.drawable.resouback_unpress); GradientDrawable myGrad_press = (GradientDrawable) getResources().getDrawable(R.drawable.resouback_pressed); //設置背景 myGrad_unpress.setColor(normalBackgroundColor); myGrad_press.setColor(pressBackgroundColor); //設置邊線 myGrad_press.setStroke(mStrokeWidth,pressStrokeColor); myGrad_unpress.setStroke(mStrokeWidth,normalStrokeColor); //設置雙線性過濾 myGrad_press.setDither(true); myGrad_unpress.setDither(true); //設置狀態加-的爲不按的效果 mBackgrounddrawable.addState(new int[]{-android.R.attr.state_pressed},myGrad_unpress); mBackgrounddrawable.addState(new int[]{android.R.attr.state_pressed},myGrad_press);
int[] colors = new int[] { pressed, normal }; int[][] states = new int[2][]; //每個二維數組均可以添加不少狀態 states[0] = new int[] { android.R.attr.state_pressed}; states[1] = new int[] { -android.R.attr.state_pressed}; ColorStateList colorList = new ColorStateList(states, colors);
textView.setBackground(mBackgrounddrawable); textView.setTextColor(colorList);
public interface ResouViewItemClickListener { /** * * @param view 返回點擊的TextView * @param index 返回點擊item的索引默認從0開始 */ void onItemClick(TextView view,int index); }
private ResouViewItemClickListener mResouViewItemClickListener; public ResouViewItemClickListener getResouViewItemClickListener() { return mResouViewItemClickListener; } /** * * @param resouViewItemClickListener item點擊事件 */ public void setResouViewItemClickListener(ResouViewItemClickListener resouViewItemClickListener) { mResouViewItemClickListener = resouViewItemClickListener; }
final int finalX = x; textView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mResouViewItemClickListener != null) { //返回textView mResouViewItemClickListener.onItemClick((TextView) v, finalX); } } });
mMyResouView = (MyResouView) findViewById(R.id.myview); mMyResouView.setNormalTextColor(Color.BLUE); mMyResouView.setPressTextColor(Color.GREEN); mMyResouView.setTextSize(20); mMyResouView.setStrokeWidth(1); mMyResouView.setResouViewItemClickListener(new ResouViewItemClickListener() { @Override public void onItemClick(TextView view, int index) { med.setText(view.getText()); Toast.makeText(MainActivity.this, ""+index, Toast.LENGTH_SHORT).show(); } });