100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 自定义控件 流式布局

自定义控件 流式布局

时间:2021-09-26 11:39:09

相关推荐

自定义控件 流式布局

自定义控件 流式布局

package com.example.layout;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.support.annotation.NonNull;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;import android.widget.FrameLayout;import android.widget.TextView;import android.widget.Toast;import com.bwei.chenguoxing1203.MainActivity;import com.bwei.chenguoxing1203.R;public class FlowLayout extends FrameLayout {private int text_size;private int text_color;public FlowLayout(@NonNull Context context) {super(context);}public FlowLayout(@NonNull Context context, @Nullable AttributeSet attrs) {super(context, attrs);TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.flow);text_color = array.getInt(R.styleable.flow_text_color, 0xFF0000FF);text_size = array.getInt(R.styleable.flow_text_size, 0);}public FlowLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.flow);text_color = array.getInt(R.styleable.flow_text_color, 0xFF0000FF);text_size = array.getInt(R.styleable.flow_text_size, 0);}public void getData(final String data){TextView textView = (TextView) View.inflate(getContext(),R.layout.text_layout,null);textView.setText(data);textView.setTextColor(text_color);textView.setTextSize(text_size);FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);textView.setLayoutParams(layoutParams);addView(textView);textView.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getContext(),data,Toast.LENGTH_SHORT).show();}});}//重写onMeasure@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);}//重写onDraw@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);}//重写onLayout@Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);int width = getWidth();int row = 0;int disWidth = 20;for (int i = 0; i < getChildCount(); i++) {View view = getChildAt(i);int viewWidth = view.getWidth();int viewHeight = view.getHeight();if (disWidth+viewWidth>width){row++;disWidth=20;}view.layout(disWidth,row*viewHeight,disWidth+viewWidth,(row+1)*viewHeight);disWidth+=viewWidth;}}}

文字布局

<TextView xmlns:android="/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="20dp"android:textSize="20dp"android:text="标题">

自定义属性

<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="flow"><attr name="text_size" format="integer"/><attr name="text_color" format="integer"/></declare-styleable></resources>

使用控件

<com.bwei.layout.FlowLayoutandroid:id="@+id/flow_1"android:layout_width="match_parent"android:layout_height="200dp"app:text_size="20"app:text_color="0xFFFF0000"></com.bwei.layout.FlowLayout>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。