解决Android Studio xml 格式化不自动换行的问题
作者:qq_41818634 发布时间:2021-09-10 06:15:42
标签:Android,Studio,xml,格式化,换行
今天把Android Studio 2.3 更新为了3.0 遇到一个蛋疼的问题
如图:
格式化完代码后发现不会自动换行了,看着真心不爽。
后来发现其实是设置问题,如图:
只要把这里打上√就可以了。
在此记录一下,希望可以帮到后面的小伙伴
补充知识:Android实现控件内自动换行(比如LinearLayout内部实现子控件换行 )
一、创建类AntoLineUtil(换行操作主要在这里实现)
package com.inpor.fmctv.util;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import com.inpor.fmctv.R;
public class AntoLineUtil extends ViewGroup {
/**
* 子view左右间距
*/
private int mHorizontalSpacing;
/**
* 子view上下行距离
*/
private int mVerticalSpacing;
private Context context;
public AntoLineUtil(Context context) {
this(context, null);
this.context = context;
}
public AntoLineUtil(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AntoLineUtil(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
if (attrs != null) {
TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.AntoLineUtil);
mHorizontalSpacing = array.getDimensionPixelOffset(
R.styleable.AntoLineUtil_horizontalSpacing, 0);
mVerticalSpacing = array.getDimensionPixelOffset(
R.styleable.AntoLineUtil_verticalSpacing, 0);
array.recycle();
if (mHorizontalSpacing < 0) mHorizontalSpacing = 0;
if (mVerticalSpacing < 0) mVerticalSpacing = 0;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int count = getChildCount();
for (int i = 0; i < count; i++) {
measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
}
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
if (widthMode != MeasureSpec.EXACTLY) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(
getAutoLinefeedWidth(width), widthMode);
}
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(
getAutoLinefeedHeight(width), heightMode);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/**
* 自动换行 计算需要的宽度
*
* @param width 可用宽度
* @return 需要的宽度
*/
private int getAutoLinefeedWidth(int width) {
int totalWidth = getPaddingLeft() + getPaddingRight();
for (int i = 0; i < getChildCount(); i++) {
if (i > 0) totalWidth += mHorizontalSpacing;
View child = getChildAt(i);
int childWidth = child.getMeasuredWidth();
totalWidth += childWidth;
if (totalWidth >= width) {
totalWidth = width;
break;
}
}
return totalWidth;
}
/**
* 自动换行 计算需要的高度
*
* @param width 可用宽度
* @return 需要的高度
*/
private int getAutoLinefeedHeight(int width) {
//一行最大可用宽度
int lineWidth = width - getPaddingLeft() - getPaddingRight();
//剩余可用宽度
int availableLineWidth = lineWidth;
//需要的高度
int totalHeight = getPaddingTop() + getPaddingBottom();
int lineChildIndex = 0;
//本行最大高度
int lineMaxHeight = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
//这个child需要的宽度 如果不是第一位的 那么需要加上间距
//这里是用来判断需不需要换行
int needWidth = i == 0 ? childWidth : (childWidth + mHorizontalSpacing);
//如果剩余可用宽度小于需要的长度 那么换行
if (availableLineWidth < needWidth) {
totalHeight = totalHeight + lineMaxHeight;
if (i > 0) totalHeight += mVerticalSpacing;
availableLineWidth = lineWidth;
lineMaxHeight = 0;
lineChildIndex = 0;
}
//这个child需要的宽度 如果不是第一位的 那么需要加上间距
int realNeedWidth = lineChildIndex == 0 ? childWidth : (childWidth + mHorizontalSpacing);
lineMaxHeight = Math.max(childHeight, lineMaxHeight);
availableLineWidth = availableLineWidth - realNeedWidth;
lineChildIndex++;
}
totalHeight = totalHeight + lineMaxHeight;
return totalHeight;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
layout();
}
private void layout() {
int count = getChildCount();
int childLeft = getPaddingLeft();
int childTop = getPaddingTop();
int lineWidth = getMeasuredWidth() - getPaddingRight() - getPaddingLeft();
int availableLineWidth = lineWidth;
int lineChildIndex = 0;
//一行的最大高度
int lineMaxHeight = 0;
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
int needWidth = i == 0 ? childWidth : (childWidth + mHorizontalSpacing);
if (availableLineWidth < needWidth) {
availableLineWidth = lineWidth;
childTop += lineMaxHeight;
if (i > 0) childTop += mVerticalSpacing;
lineMaxHeight = 0;
childLeft = getPaddingLeft();
lineChildIndex = 0;
}
int realNeedWidth = lineChildIndex == 0 ? childWidth : (childWidth + mHorizontalSpacing);
lineMaxHeight = Math.max(lineMaxHeight, childHeight);
child.layout(childLeft + realNeedWidth - childWidth, childTop, childLeft + realNeedWidth, childTop + childHeight);
availableLineWidth -= realNeedWidth;
childLeft += realNeedWidth;
lineChildIndex++;
}
}
public int getHorizontalSpacing() {
return mHorizontalSpacing;
}
public void setHorizontalSpacing(int horizontalSpacing) {
mHorizontalSpacing = horizontalSpacing;
}
public int getVerticalSpacing() {
return mVerticalSpacing;
}
public void setVerticalSpacing(int verticalSpacing) {
mVerticalSpacing = verticalSpacing;
}
}
二、在values中的attrs.xml中添加以下代码(实现子控件的边距):
<declare-styleable name="AntoLineUtil">
<attr name="horizontalSpacing" format="dimension"/>
<attr name="verticalSpacing" format="dimension"/>
</declare-styleable>
三、添加固定的xml布局父控件,事先写好,布局activity_video_preview.xml :
<com.inpor.fmctv.util.AntoLineUtil
android:id="@+id/camera_group"
android:layout_width="@dimen/size_dp_630"
android:layout_height="@dimen/size_dp_138"
android:layout_marginTop="@dimen/size_dp_18"
android:orientation="horizontal"
app:horizontalSpacing="@dimen/size_dp_18"
app:verticalSpacing="@dimen/size_dp_18">
</com.inpor.fmctv.util.AntoLineUtil>
四、添加固定的xml布局子控件,事先写好,动态添加进去,布局item_camera_info.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/video_preview_item"
android:layout_width="@dimen/size_dp_198"
android:layout_height="@dimen/size_dp_60"
android:orientation="horizontal"
android:paddingLeft="@dimen/size_dp_18"
android:paddingRight="@dimen/size_dp_18"
android:gravity="center_vertical"
android:background="@color/textcolor_395878">
<TextView
android:id="@+id/video_preview_item_tv"
android:layout_width="@dimen/size_dp_120"
android:layout_height="wrap_content"
android:textSize="@dimen/size_sp_24"
android:textColor="@color/white"/>
<CheckBox
android:id="@+id/video_previ"
android:layout_width="@dimen/size_dp_24"
android:layout_height="@dimen/size_dp_24"
android:button="@null"
android:background="@drawable/radio_button_select_ico" />
</LinearLayout>
五、在其他方法中动态添加子控件:
AntoLineUtil cameraGroup = (AntoLineUitl) findViewById(R.id.camera_group); // 此处是找到父控件LinearLayout
for (int i = 0; i<6; i++) {
// 用以下方法将layout布局文件换成view
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.item_camera_info,null);
TextView textView = view.findViewById(R.id.video_preview_item_tv);
textView.setText("摄像头"+ (cameraId+1));
cameraGroup.addView(view);
}
六、效果图:
来源:https://blog.csdn.net/qq_41818634/article/details/79510382
0
投稿
猜你喜欢
- 随着C#的发展,该语言内容不断丰富,开发变得更加方便快捷,C# 的锋利尽显无疑。C# 语言从诞生起就是强类型语言,这一性质到今天不曾改变,我
- 目录源码使用源码@Target({ElementType.TYPE, ElementType.METHOD})@Retention(Rete
- 本文实例讲述了Android编程之手机壁纸WallPaper设置方法。分享给大家供大家参考,具体如下:/** * Andorid设置手机屏幕
- 1.申请测试号,并记录appID和appsecret2.关注测试号3.添加消息模板{{topic.DATA}} 用户名: {{user.DA
- Map映射Map映射是一个java集合存储在键 - 值对的元素,并且不允许在列表中重复的元素。 Map接口提供三种collection视图,
- //执行顺序:(优先级从高到低。)静态代码块>mian方法>构造代码块>构造方法。其中静态代码块只执行一次。构造代码块在每
- 1.引言在开发中,拖放是一种比较常见的手势操作,使用它能够让应用的交互更加地便捷和友好,本文将简要介绍如何为Android中的View添加拖
- 本文实例讲述了Java泛型的使用限制。分享给大家供大家参考,具体如下:一 什么情况下不能使用泛型1 不能使用泛型的形参创建对象。T o=ne
- 用DataFormatString格式化GridView在 GridView里面显示数据,要显示的数据有好多位小数,就想让它只显示两位小数,
- intellij idea是一款非常优秀的软件开发工具,它拥有这强大的插件体系,可以帮助开发者完成很多重量级的功能。今天,我们来学习一下如何
- Java当中的类和对象1. 类和对象的初步认知java 是一门面向对象的语言,所谓面向对象有别于面向过程,面向对象是只需对象之间的交互即可完
- 一、简介(1)、MySQL是一个关系型数据库系统,是如今互联网公司最常用的数据库和最广泛的数据库。为服务端数据库,能承受高并发的访问量。(2
- springmvc 使用map接收参数开发过程中有时候我们并不知道前端都会传递哪些参数给到后端. 为方便扩展接口功能, 在请求参数不改变的情
- 1.多数元素题目描述思路详解这个思路比较简单,先排序,排序过后遍历如果后一个等于前一个输出就好代码与结果class Solution { &
- MVC三层架构我们在刚刚成为程序员的时候,就会被前辈们 “教育” 说系统的设计要遵循 MVC(Model-View-Controller)架
- 前言通过深入分析Spring源码,我们知道Spring框架包括大致六大模块, 如Web模块,数据库访问技术模块,面向切面模块,基础设施模块,
- 这篇文章主要介绍了Java TreeSet类的简单理解和使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 你是否受够了每次修改静态文件都要重启服务器?有时候在一些公司前后端的职责没有那么的明确,往往后台人员也要去写一些页面,像jsp页面,或者其他
- 一、注解是什么Java 注解用于为 Java 代码提供元数据,看完这句话也许你还是一脸懵逼,用人话说就是注解不直接影响你的代码执行,仅提供信
- Spring之动态注册bean什么场景下,需要主动向Spring容器注册bean呢?如我之前做个的一个支持扫表的基础平台,使用者只需要添加基