//自定义标题栏 public class MyTitleView extends LinearLayout implements View.OnClickListener { public MyTitleView(Context context) { super(context); } public MyTitleView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyTiltleView); String title_text = array.getString(R.styleable.MyTiltleView_title_text); String btn_left = array.getString(R.styleable.MyTiltleView_btn_left_text); String btn_right = array.getString(R.styleable.MyTiltleView_btn_right_text); View view = inflate(context, R.layout.title_layout, this); //获取id Button left_btn = view.findViewById(R.id.btn_left); Button right_btn= view.findViewById(R.id.btn_right); TextView text_title=view.findViewById(R.id.title_tet); left_btn.setOnClickListener(this); right_btn.setOnClickListener(this); text_title.setOnClickListener(this); } //点击事件 @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_left: if(onBtnOnClick!=null){ onBtnOnClick.onLeftClick(); } break; case R.id.btn_right: if(onBtnOnClick!=null){ onBtnOnClick.onRIghtClick(); } break; case R.id.title_tet: if(onBtnOnClick!=null){ onBtnOnClick.getText(); } break; } } //接口回调 public interface onBtnOnClick{ void onLeftClick(); void onRIghtClick(); void getText(); } //设置外部访问的方法 private onBtnOnClick onBtnOnClick; public void onBtnOnClick(MyTitleView.onBtnOnClick onBtnOnClick){ this.onBtnOnClick=onBtnOnClick; } }
//梯形布局
public class FlowLayout extends ViewGroup { public FlowLayout(Context context) { super(context); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //获取子控件宽度与高度 measureChildren(widthMeasureSpec, heightMeasureSpec); int widthmode = MeasureSpec.getMode(widthMeasureSpec); int widthsize = MeasureSpec.getSize(widthMeasureSpec); int heightmode = MeasureSpec.getMode(heightMeasureSpec); int heightsize = MeasureSpec.getSize(heightMeasureSpec); int width = 0; int height = 0; int linewidth = 0; int lineheight = 0; int totalheight = 0; View childview; int childwidth = 0; int childheght = 60; if (getChildCount() == 0) { setMeasuredDimension(0, 0); } for (int i = 0; i < getChildCount(); i++) { childview = getChildAt(i); childwidth = childview.getMeasuredWidth(); if (childwidth > widthsize) { throw new IllegalArgumentException("子view宽度不能大于FlowLayout宽度"); } //得到子控件的高度 childheght = childview.getMeasuredHeight(); //如果每行的子控件的宽度大于父控件的话,进行换行 if (linewidth + childwidth > widthsize) { //换行之后 width = widthsize; //当前的高度等于新来的子控件的高度 totalheight += lineheight; lineheight = childheght; } else { //否则的话不换行,往后进行拼接 linewidth += childwidth; lineheight = Math.max(lineheight, childheght); width = Math.max(width, linewidth); } if (i == getChildCount() - 1) { totalheight += lineheight; height = totalheight; } width = widthmode == MeasureSpec.EXACTLY ? widthsize : width; height = heightmode == MeasureSpec.EXACTLY ? heightsize : width; //确定最终测量的宽高 setMeasuredDimension(width, height); } } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int width = 0; int height = 0; int linewidth = 0; int lineheight = 0; int totalheight = 0; View childview; int childwidth = 0; int childheght = 60; for (int i = 0; i < getChildCount(); i++) { childview = getChildAt(i); childwidth = childview.getMeasuredWidth(); childheght = childview.getMeasuredHeight(); if (i % 2 == 0) { totalheight += childheght; childwidth =getWidth()/2; linewidth = 0; layoutChildView(childview, linewidth, totalheight, linewidth + childwidth, totalheight + childheght); } else { totalheight += childheght; childwidth =getWidth()/2; linewidth = childwidth ;; layoutChildView(childview, linewidth, totalheight, linewidth + childwidth, totalheight + childheght); } } } public void layoutChildView(View child, int l, int h, int r, int b) { child.layout(l, h, r, b); } }
//MainActivity
public class MainActivity extends AppCompatActivity { private MyTitleView title; private FlowLayout flow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); title = findViewById(R.id.mytitle); flow = findViewById(R.id.flow); title.onBtnOnClick(new MyTitleView.onBtnOnClick() { @Override public void onLeftClick() { boolean flag; if(flow.getChildAt(0)!=null){ flag=true; }else{ flag=false; Toast.makeText(MainActivity.this,"数据已清空",Toast.LENGTH_SHORT).show(); } if(flag==true){ flow.removeViewAt(0); } } @Override public void onRIghtClick() { Toast.makeText(MainActivity.this,"+",Toast.LENGTH_SHORT).show(); TextView text = new TextView(MainActivity.this); text.setText("赵丽颖"); text.setTextSize(25); text.setBackgroundColor(Color.RED); text.setWidth(flow.getWidth()/2); text.setHeight(60); flow.addView(text); } @Override public void getText() { // flow.removeAllViews(); } }); } }
//自定义属性
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyTiltleView"> <attr name="title_text" format="string"></attr> <attr name="btn_left_text" format="string"></attr> <attr name="btn_right_text" format="string"></attr> </declare-styleable> </resources>
//
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" > <com.bway.day_0609_zhouliumoni.MyTitleView android:id="@+id/mytitle" android:layout_width="match_parent" android:layout_height="wrap_content" app:title_text="清空" app:btn_left_text="-" app:btn_right_text="+" android:background="@color/colorAccent" > </com.bway.day_0609_zhouliumoni.MyTitleView> <com.bway.day_0609_zhouliumoni.FlowLayout android:id="@+id/flow" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="60dp" android:text="first" android:gravity="center" android:textSize="25dp" android:background="#ff5656" /> <TextView android:layout_width="wrap_content" android:layout_height="60dp" android:text="second" android:gravity="center" android:textSize="25dp" android:background="#fff566" /> <TextView android:layout_width="wrap_content" android:layout_height="60dp" android:text="third" android:gravity="center" android:textSize="25dp" android:background="#56ff66" /> <TextView android:layout_width="wrap_content" android:layout_height="60dp" android:text="fourth" android:gravity="center" android:textSize="25dp" android:background="#0099ff" /> <TextView android:layout_width="wrap_content" android:layout_height="60dp" android:text="fifth" android:gravity="center" android:textSize="25dp" android:background="#ff56ff" /> <TextView android:layout_width="wrap_content" android:layout_height="60dp" android:text="sixth" android:gravity="center" android:textSize="25dp" android:background="#ff56ff" /> <TextView android:layout_width="wrap_content" android:layout_height="60dp" android:text="seventh" android:gravity="center" android:textSize="25dp" android:background="#ff56ff" /> <TextView android:layout_width="wrap_content" android:layout_height="60dp" android:text="eighth" android:gravity="center" android:textSize="25dp" android:background="#ff56ff" /> </com.bway.day_0609_zhouliumoni.FlowLayout> </LinearLayout>
//、、、、
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/btn_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:layout_marginLeft="20dp" /> <TextView android:id="@+id/title_tet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清空" android:layout_marginLeft="20dp" android:gravity="center" /> <Button android:id="@+id/btn_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:layout_marginLeft="20dp" /> </LinearLayout>