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

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

时间:2021-12-26 10:36:37

相关推荐

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

自定义组合控件

package .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 .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);

}

}

xmlns:android="/apk/res/android" android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/two_layout_id"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

android:id="@+id/two_ed_id"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

/>

android:id="@+id/two_text_id"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="搜索"

android:textSize="20sp"

android:background="#4ff"

/>

android:id="@+id/two_liu_id"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#ccc">

activity

package .day09.two;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import .day09.R;

public class TwoActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_two);

}

}

xmlns:app="/apk/res-auto"

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".two.TwoActivity">

android:layout_width="match_parent"

android:layout_height="match_parent">

流式布局

package .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;

}

}

}

标签:控件,自定义,int,context,import,Android,leftMargin,public,android

来源: /qq_42886907/article/details/89064858

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