广告位联系
返回顶部
分享到

解决Android Studio xml 格式化不自动换行的问题

Android 来源:互联网搜集 作者:秩名 发布时间:2020-03-17 20:12:20 人浏览
摘要

今天把Android Studio 2.3 更新为了3.0 遇到一个蛋疼的问题 如图: 格式化完代码后发现不会自动换行了,看着真心不爽。 后来发现其实是设置问题,如图: 只要把这里打上就可以了。 在此记录一下,希望可以帮到后面的小伙伴 补充知识: Android实现控件内自动

今天把Android Studio 2.3 更新为了3.0 遇到一个蛋疼的问题

如图:

-[-/a>

格式化完代码后发现不会自动换行了,看着真心不爽。

后来发现其实是设置问题,如图:

-[-/a>

只要把这里打上√就可以了。

-[-/a>

在此记录一下,希望可以帮到后面的小伙伴

补充知识:Android实现控件内自动换行(比如LinearLayout内部实现子控件换行 )

一、创建类AntoLineUtil(换行操作主要在这里实现)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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中添加以下代码(实现子控件的边距):

?
1
2
3
4
<declare-styleable name="AntoLineUtil">
    <attr name="horizontalSpacing" format="dimension"/>
    <attr name="verticalSpacing" format="dimension"/>
  </declare-styleable>

三、添加固定的xml布局父控件,事先写好,布局activity_video_preview.xml :

?
1
2
3
4
5
6
7
8
9
<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 :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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>

五、在其他方法中动态添加子控件:

?
1
2
3
4
5
6
7
8
9
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);
}

六、效果图:


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : 原文链接:https://blog.csdn.net/qq_41818634/article/details/79510382
相关文章
  • Kotlin的Collection与Sequence操作异同点介绍

    Kotlin的Collection与Sequence操作异同点介绍
    在Android开发中,集合是我们必备的容器,Kotlin的标准库中提供了很多处理集合的方法,而且还提供了两种基于容器的工作方式:Collection 和
  • 实现一个Kotlin函数类型方法

    实现一个Kotlin函数类型方法
    接口与函数类型 业务开发中,经常会有实现一个函数式接口(即接口只有一个方法需要实现)的场景,大家应该都会不假思索的写出如下代
  • Android10 App启动Activity源码分析
    ActivityThread的main方法 让我们把目光聚焦到ActivityThread的main方法上。 ActivityThread的源码路径为/frameworks/base/core/java/android/app/ActivityThread。 1 2
  • Android10客户端事务管理ClientLifecycleManager源码解析

    Android10客户端事务管理ClientLifecycleManager源码解析
    在Android 10 App启动分析之Activity启动篇(二)一文中,简单地介绍了Activity的生命周期管理器是如何调度Activity进入onCreate生命周期的流程。这
  • Kotlin对象的懒加载方式by lazy与lateinit异同介绍

    Kotlin对象的懒加载方式by lazy与lateinit异同介绍
    属性或对象的延时加载是我们相当常用的,一般我们都是使用 lateinit 和 by lazy 来实现。 他们两者都是延时初始化,那么在使用时那么他们两
  • Android类加载流程分析

    Android类加载流程分析
    本文分析的代码基于Android8.1.0源码。 流程分析 从loadClass开始,我们来看下Android中类加载的流程 /libcore/ojluni/src/main/java/java/lang/ClassLoader.ja
  • Android实现读写USB串口数据的代码

    Android实现读写USB串口数据的代码
    最近在研究USB方面的内容;先后做了关于Android读写HID、串口设备的DEMO。本文比较简单,主要介绍的是Android实现读取串口数据的功能 废话不
  • Epoxy - 在RecyclerView中构建复杂界面
    Diffing 对于复杂数据结构支持的多个视图类型展示在屏幕上, Epoxy此时是尤其有用的. 在这些场景中, 数据可能会被网络请求, 异步 Observable, 用
  • Android性能优化的详细介绍

    Android性能优化的详细介绍
    性能优化是一个app很重要的一部分,一个性能优良的app从被下载到启动到使用都能给用户到来很好的体验。自然我们做性能优化也是从被下
  • Android进阶宝典-插件化2(Hook启动插件中四大组件

    Android进阶宝典-插件化2(Hook启动插件中四大组件
    在上一节,我们主要介绍了如果通过反射来加载插件中的类,调用类中的方法;既然插件是一个apk,其实最重要的是启动插件中的Activity、
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计