自定義流式佈局
阿新 • • 發佈:2018-12-11
(一)MainActivity主佈局
<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" android:orientation="vertical" tools:context="zhangyanran201800920.bwie.com.gouwuche.activity.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <FrameLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"> <EditText android:id="@+id/edt" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入內容"/> </FrameLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/but" android:text="搜尋" android:background="#808080"/> </LinearLayout> //實現自定義佈局 <zhangyanran201800920.bwie.com.gouwuche.widget.CustomView android:id="@+id/afl_cotent" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
(二)MainActivity邏輯程式碼
public class MainActivity extends AppCompatActivity { private EditText edt; private Button but; private CustomView afl_cotent; private LayoutInflater layoutInflater; private TextView tv_attr_tag; private List<String> list = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { //初始化控制元件 edt = findViewById(R.id.edt); but = findViewById(R.id.but); afl_cotent = findViewById(R.id.afl_cotent); //載入佈局 layoutInflater = LayoutInflater.from(this); //點選事件 but.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //獲取edit內容 String s = edt.getText().toString(); //載入子佈局 View item = layoutInflater.inflate(R.layout.sub_item, null); tv_attr_tag = item.findViewById(R.id.tv_attr_tag); list.add(s); for (int i = 0; i < list.size(); i++) { tv_attr_tag.setText(list.get(i)); tv_attr_tag.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, CartActivity.class); startActivity(intent); } }); } afl_cotent.addView(item); } }); } }
(二.1)記載子條目的佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_attr_tag" android:layout_margin="10dp" /> </LinearLayout>
(三)自定義類的邏輯程式碼
public class CustomView extends ViewGroup {
private int mleftMargin=20;
private int mtopMargin=20;
public CustomView(Context context) {
this(context,null);
}
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public CustomView(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 leftMargin = mleftMargin;
int topMargin = mtopMargin;
//父控制元件傳進來的寬度和高度以及對應的測量模式
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
switch (modeHeight){
case MeasureSpec.AT_MOST:
int measuredHeight = 0;
for (int j = 0; j < getChildCount(); j++) {
int measuredWidth = getChildAt(j).getMeasuredWidth();
measuredHeight = getChildAt(j).getMeasuredHeight();
if (leftMargin+measuredWidth+mleftMargin>getWidth()){
leftMargin=mleftMargin;
topMargin+=measuredHeight+mtopMargin;
}
leftMargin+=measuredWidth+mleftMargin;
}
topMargin+=measuredHeight+mtopMargin;
break;
}
setMeasuredDimension(sizeWidth,topMargin);
}
@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
int leftMargin = mleftMargin;
int topMargin = mtopMargin;
for (int j = 0; j < getChildCount(); j++) {
int measuredWidth = getChildAt(j).getMeasuredWidth();
int measuredHeight = getChildAt(j).getMeasuredHeight();
if (leftMargin+measuredWidth+mleftMargin>getWidth()){
leftMargin=mleftMargin;
topMargin+=measuredHeight+mtopMargin;
getChildAt(j).layout(leftMargin,topMargin,leftMargin+measuredWidth,topMargin+measuredHeight);
}else {
getChildAt(j).layout(leftMargin,topMargin,leftMargin+measuredWidth,topMargin+measuredHeight);
}
leftMargin+=measuredWidth+mleftMargin;
}
}
}