解決Android Studio xml 格式化不自動換行的問題
今天把Android Studio 2.3 更新為了3.0 遇到一個問題
如圖:

格式化完代碼后發(fā)現(xiàn)不會自動換行了,看著真心不爽。
后來發(fā)現(xiàn)其實是設(shè)置問題,如圖:

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

在此記錄一下,希望可以幫到后面的小伙伴
補充知識:Android實現(xiàn)控件內(nèi)自動換行(比如LinearLayout內(nèi)部實現(xiàn)子控件換行 )
一、創(chuàng)建類AntoLineUtil(換行操作主要在這里實現(xiàn))
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中添加以下代碼(實現(xiàn)子控件的邊距):
<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布局子控件,事先寫好,動態(tài)添加進去,布局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>五、在其他方法中動態(tài)添加子控件:
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);
}六、效果圖:

以上這篇解決Android Studio xml 格式化不自動換行的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- AndroidStudio修改Code Style來格式化自定義標簽的xml文件方式
- 解決Android Studio 格式化 Format代碼快捷鍵問題
- Android Studio實現(xiàn)格式化XML代碼順序
- AndroidStudio 設(shè)置格式化斷行寬度教程
- Android Studio 3.5格式化布局代碼時錯位、錯亂bug的解決
- Android studio kotlin代碼格式化操作
- 解決Android Studio 格式化快捷鍵和QQ 鎖鍵盤快捷鍵沖突問題
- Android 國際貨幣格式化的示例代碼
- Android中使用 AutoCompleteTextView 實現(xiàn)手機號格式化附帶清空歷史的操作
- Android實現(xiàn)的數(shù)字格式化用法示例
- Android 優(yōu)雅的實現(xiàn)通用格式化編輯
相關(guān)文章
Android編程實現(xiàn)創(chuàng)建,刪除,判斷快捷方式的方法
這篇文章主要介紹了Android編程實現(xiàn)創(chuàng)建,刪除,判斷快捷方式的方法,結(jié)合實例形式分析了Android編程針對快捷方式的常用操作技巧,需要的朋友可以參考下2017-02-02
Android啟動頁廣告(騰訊廣告聯(lián)盟)解決方法及源碼下載
這篇文章主要介紹了Android啟動頁廣告(騰訊廣告聯(lián)盟)解決方法的相關(guān)資料,啟動頁幾乎成為了每個app的標配,有些商家在啟動頁中增加了開屏廣告以此帶來更多的收入,需要的朋友可以參考下2017-03-03
Android編程心得分享——JSON學(xué)習(xí)過程
在我們初步學(xué)習(xí)JSON時我們都知道JSON作為現(xiàn)在比較流行的數(shù)據(jù)交換格式,有著它的許多優(yōu)點,這里將我學(xué)習(xí)JSON的過程記錄如下2013-06-06
Android webview和js互相調(diào)用實現(xiàn)方法
這篇文章主要介紹了 Android webview和js互相調(diào)用實現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android編程實現(xiàn)屏幕自適應(yīng)方向尺寸與分辨率的方法
這篇文章主要介紹了Android編程實現(xiàn)屏幕自適應(yīng)方向尺寸與分辨率的方法,涉及Android屏幕分辨率、布局、橫豎屏切換等相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12
使用Android Studio創(chuàng)建OpenCV4.1.0 項目的步驟
這篇文章主要介紹了使用Android Studio創(chuàng)建OpenCV4.1.0 項目的步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

