自定义ViewGroup实现换行
程序员文章站
2022-06-08 17:34:42
...
1.AutoViewGroup
package com.example.yangdechengapplication.view;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
public class AutoViewGroup extends ViewGroup {
private int mScreenWidth;//屏幕的宽度
private int mHorizontalSpacing;//横向的自view的间距
private int mVerticalSpacing;//行间距
public AutoViewGroup(Context context) {
super(context);
}
public AutoViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void setSpacing(int horizontalSpacing, int verticalSpacing) {
mHorizontalSpacing = horizontalSpacing;
mVerticalSpacing = verticalSpacing;
}
private void init() {
DisplayMetrics dm = getResources().getDisplayMetrics();
mScreenWidth = dm.widthPixels;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
//也可以根据根据孩子的大小计算出自身的宽高
////根据自身的宽度约束子控件宽度
measureChildren(widthMeasureSpec, heightMeasureSpec);
//设置自身宽度(默认的宽高是充满屏幕的)
setMeasuredDimension(widthSize, heightSize);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int mTotalHeight = 0;
int mTotalWidth = 0;
int mTempHeight = 0;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
int measureHeight = childView.getMeasuredHeight();
int measuredWidth = childView.getMeasuredWidth();
mTempHeight = (measureHeight > mTempHeight) ? measureHeight : mTempHeight;
if ((measuredWidth + mTotalWidth + mHorizontalSpacing) > mScreenWidth) {
mTotalWidth = 0;
mTotalHeight += (mTempHeight + mVerticalSpacing);
mTempHeight = 0;
}
childView.layout(mTotalWidth + mHorizontalSpacing, mTotalHeight, measuredWidth + mTotalWidth + mHorizontalSpacing, mTotalHeight + measureHeight);
mTotalWidth += (measuredWidth + mHorizontalSpacing);
int a=0;
String x="";
}
}
}
2.Activity
package com.example.yangdechengapplication.customview;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.example.yangdechengapplication.R;
import com.example.yangdechengapplication.view.AutoViewGroup;
import java.util.ArrayList;
public class CustomviewActivity extends AppCompatActivity {
private ArrayList<String> mData = new ArrayList<>();
private Context mContext;
private AutoViewGroup autoBreakViewGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.activity_customview_test);
autoBreakViewGroup = (AutoViewGroup) findViewById(R.id.autoBreakViewGroup);
autoBreakViewGroup.setSpacing(20, 20);
initView();
}
private void initView() {
mData.add("猪八戒");
mData.add("刘备");
mData.add("无能");
mData.add("旋风少女");
mData.add("孙悟空");
mData.add("拉玛西亚");
for (int i = 0; i < mData.size(); i++) {
TextView textView = new TextView(mContext);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Toast在其他线程中调用显示", Toast.LENGTH_SHORT).show();
}
});
textView.setText(mData.get(i));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
textView.setTextColor(Color.BLACK);
textView.setLayoutParams(new
ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setBackgroundResource(R.drawable.btn_radius_blue_bg);
textView.setPadding(20, 10, 20, 10);
textView.setGravity(Gravity.CENTER);
autoBreakViewGroup.addView(textView);
}
}
}
3.XML布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.example.yangdechengapplication"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.yangdechengapplication.view.CustomTitleView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:titleText="3712"
android:padding="10dp"
custom:titleTextColor="#ff0000"
android:layout_centerInParent="true"
custom:titleTextSize="40sp" />
</RelativeLayout>
推荐阅读
-
Android自定义控件实现icon+文字的多种效果
-
Android编程实现自定义ProgressBar样式示例(背景色及一级、二级进度条颜色)
-
Android使用自定义alertdialog实现确认退出按钮
-
Android自定义view实现进度条指示效果
-
ASP.NET通过自定义函数实现对字符串的大小写切换功能
-
自定义的Troop
泛型类( c++, java和c#)的实现代码 -
Android自定义dialog简单实现方法
-
实例详解Android自定义ProgressDialog进度条对话框的实现
-
Android百度地图实现搜索和定位及自定义图标绘制并点击时弹出泡泡
-
超简单实现Android自定义Toast示例(附源码)