电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

解決Android Studio xml 格式化不自動換行的問題

瀏覽:127日期:2022-09-26 14:19:45

今天把Android Studio 2.3 更新為了3.0 遇到一個蛋疼的問題

如圖:

解決Android Studio xml 格式化不自動換行的問題

格式化完代碼后發現不會自動換行了,看著真心不爽。

后來發現其實是設置問題,如圖:

解決Android Studio xml 格式化不自動換行的問題

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

解決Android Studio xml 格式化不自動換行的問題

在此記錄一下,希望可以幫到后面的小伙伴

補充知識: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: 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: 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: android:layout_width='@dimen/size_dp_120' android:layout_height='wrap_content' android:textSize='@dimen/size_sp_24' android:textColor='@color/white'/> <CheckBox android: 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); // 此處是找到父控件LinearLayoutfor (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 格式化不自動換行的問題

以上這篇解決Android Studio xml 格式化不自動換行的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 变位机,焊接变位机,焊接变位器,小型变位机,小型焊接变位机-济南上弘机电设备有限公司 | 柴油发电机组_柴油发电机_发电机组价格-江苏凯晨电力设备有限公司 | 美国PARKER齿轮泵,美国PARKER柱塞泵,美国PARKER叶片泵,美国PARKER电磁阀,美国PARKER比例阀-上海维特锐实业发展有限公司二部 | 活性氧化铝球|氧化铝干燥剂|分子筛干燥剂|氢氧化铝粉-淄博同心材料有限公司 | 工业rfid读写器_RFID工业读写器_工业rfid设备厂商-ANDEAWELL | DAIKIN电磁阀-意大利ATOS电磁阀-上海乾拓贸易有限公司 | 天津云仓-天津仓储物流-天津云仓一件代发-顺东云仓 | 移动机器人产业联盟官网| 工程管道/塑料管材/pvc排水管/ppr给水管/pe双壁波纹管等品牌管材批发厂家-河南洁尔康建材 | 上海阳光泵业制造有限公司 -【官方网站】 | 紫外可见光分光度计-紫外分光度计-分光光度仪-屹谱仪器制造(上海)有限公司 | 三氯异氰尿酸-二氯-三氯-二氯异氰尿酸钠-优氯净-强氯精-消毒片-济南中北_优氯净厂家 | 水质监测站_水质在线分析仪_水质自动监测系统_多参数水质在线监测仪_水质传感器-山东万象环境科技有限公司 | 液压扳手-高品质液压扳手供应商 - 液压扳手, 液压扳手供应商, 德国进口液压拉马 | 压砖机、液压制砖机、静压砖机、环保砖机生产厂家—杜甫机械 | 丙烷/液氧/液氮气化器,丙烷/液氧/液氮汽化器-无锡舍勒能源科技有限公司 | 单螺旋速冻机-双螺旋-流态化-隧道式-食品速冻机厂家-广州冰泉制冷 | 纸箱抗压机,拉力机,脂肪测定仪,定氮仪-山东德瑞克仪器有限公司 | 辽宁资质代办_辽宁建筑资质办理_辽宁建筑资质延期升级_辽宁中杭资质代办 | 净水器代理,净水器招商,净水器加盟-FineSky德国法兹全屋净水 | 流量卡中心-流量卡套餐查询系统_移动电信联通流量卡套餐大全 | 谈股票-今日股票行情走势分析-牛股推荐排行榜 | 转子泵_凸轮泵_凸轮转子泵厂家-青岛罗德通用机械设备有限公司 | 武汉刮刮奖_刮刮卡印刷厂_为企业提供门票印刷_武汉合格证印刷_现金劵代金券印刷制作 - 武汉泽雅印刷有限公司 | 气动隔膜泵厂家-温州永嘉定远泵阀有限公司| 加中寰球移民官网-美国移民公司,移民机构,移民中介,移民咨询,投资移民 | 微型实验室真空泵-无油干式真空泵-微型涡旋耐腐蚀压缩机-思科涡旋科技(杭州)有限公司 | 恒温振荡混匀器-微孔板振荡器厂家-多管涡旋混匀器厂家-合肥艾本森(www.17world.net) | 【孔氏陶粒】建筑回填陶粒-南京/合肥/武汉/郑州/重庆/成都/杭州陶粒厂家 | 石家庄小程序开发_小程序开发公司_APP开发_网站制作-石家庄乘航网络科技有限公司 | 粘度计维修,在线粘度计,二手博勒飞粘度计维修|收购-天津市祥睿科技有限公司 | ptc_浴霸_大巴_干衣机_呼吸机_毛巾架_电动车加热器-上海帕克 | 柔性输送线|柔性链板|齿形链-上海赫勒输送设备有限公司首页[输送机] | 泰安塞纳春天装饰公司【网站】 | 实验室装修_实验室设计_实验室规划设计- 上海广建净化工程公司 | 压砖机_电动螺旋压力机_粉末成型压力机_郑州华隆机械tel_0371-60121717 | 深圳装修_店面装修设计_餐厅设计_装修全包价格-尚泰装饰设计 | 二手电脑回收_二手打印机回收_二手复印机回_硒鼓墨盒回收-广州益美二手电脑回收公司 | 不锈钢拉手厂家|浴室门拉手厂家|江门市蓬江区金志翔五金制品有限公司 | 太空舱_民宿太空舱厂家_移动房屋太空舱价格-豪品建筑 | 家用净水器代理批发加盟_净水机招商代理_全屋净水器定制品牌_【劳伦斯官网】 |