日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2024/7/19 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android自定义组合布局,Android 流式布局 + 自定义组合控件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

自定義組合控件

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

}

}

xmlns:android="http://schemas.android.com/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 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);

}

}

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"

tools:context=".two.TwoActivity">

android:layout_width="match_parent"

android:layout_height="match_parent">

流式布局

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;

}

}

}

標簽:控件,自定義,int,context,import,Android,leftMargin,public,android

來源: https://blog.csdn.net/qq_42886907/article/details/89064858

總結

以上是生活随笔為你收集整理的Android自定义组合布局,Android 流式布局 + 自定义组合控件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。