android 自定义组合布局,Android 流式布局 + 自定义组合控件

该博客主要围绕Android开发展开,详细给出了自定义组合控件、Activity以及流式布局的代码实现。自定义组合控件可实现特定交互功能,流式布局则对子视图进行合理布局,为Android开发者提供了实用的代码参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义组合控件 package yanjupeng.bawei.com.day09.two; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import yanjupeng.bawei.com.day09.R; public class TwoLayout extends LinearLayout { public TextView textView; public EditText editText; public TwoLiu twoLiu; public LinearLayout linearLayout; public TwoLayout(Context context) { super(context); } public TwoLayout(final Context context, AttributeSet attrs) { super(context, attrs); View view = LayoutInflater.from(context).inflate(R.layout.twolayout , this); textView = view.findViewById(R.id.two_text_id); editText = view.findViewById(R.id.two_ed_id); twoLiu = view.findViewById(R.id.two_liu_id); linearLayout = view.findViewById(R.id.two_layout_id); textView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String string = editText.getText().toString(); Log.e("tag" , "textView ====="+string ); final TextView textView = new TextView(context); textView.setTag(string); textView.setText(string); textView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.e("tag" , "textView =====" + textView.getTag().toString()); } }); twoLiu.addView(textView); linearLayout.requestLayout(); } }); } public TwoLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } } <?xml version="1.0" encoding="utf-8"?>

activity package yanjupeng.bawei.com.day09.two; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import yanjupeng.bawei.com.day09.R; public class TwoActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); } } <?xml version="1.0" encoding="utf-8"?>

流式布局 package yanjupeng.bawei.com.day09.two; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.ViewGroup; public class TwoLiu extends ViewGroup { public int mLeftMargin = 20; public int mTopMargin = 20; public TwoLiu(Context context) { super(context); } public TwoLiu(Context context, AttributeSet attrs) { super(context, attrs); } public TwoLiu(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); Log.e("tag" , "onMeasure"); measureChildren(widthMeasureSpec ,heightMeasureSpec); int leftMargin = mLeftMargin; int topMargin = mTopMargin; int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); int modelWidth = MeasureSpec.getMode(widthMeasureSpec); int sizeHeigth = MeasureSpec.getSize(heightMeasureSpec); int modelHeigth = MeasureSpec.getMode(heightMeasureSpec); switch (modelHeigth){ 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 > getMeasuredWidth()){ leftMargin = mLeftMargin; topMargin += measuredHeight + mTopMargin; } leftMargin += measuredWidth + mLeftMargin; } topMargin += measuredHeight + mTopMargin; break; } setMeasuredDimension(sizeWidth , topMargin); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { Log.e("tag" , "onLayout"); int leftMargin = mLeftMargin; int topmargin = mTopMargin; for (int j = 0 ; j < getChildCount() ; j ++){ int measuredHeight = getChildAt(j).getMeasuredHeight(); int measuredWidth = getChildAt(j).getMeasuredWidth(); if (leftMargin + measuredWidth + mLeftMargin > getWidth()){ leftMargin = mLeftMargin; topmargin += measuredHeight + mTopMargin; getChildAt(j).layout(leftMargin , topmargin , measuredWidth + leftMargin , measuredHeight + topmargin ); }else { getChildAt(j).layout(leftMargin , topmargin , measuredWidth + leftMargin ,measuredHeight + topmargin ); } leftMargin += measuredWidth + mLeftMargin; } } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值