关于LayoutParams(int width, int height, float weight) 参数解析

今天做项目的时候遇到一个非常奇葩的问题,textview不能显示。

问题如下:

        我通过textView.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1.0F));来设置textview参数的时候,在我同事的开发环境下(AS加最新版本的23.0.7的ADT)一切正常。但是,在我的开发环境下(Eclipse,同样版本的ADT),却一直不能显示textview。


分析原因:

       关键问题在第一个参数width=0,一般情况下,设置宽度为0默认为忽略宽度,按照weight来布局。在我同事的开发环境下也确实这样运行的。但在我的开发环境下,0变成了0px。导致Textview不能显示出来。


至于为什么会这样,还没搞清楚,先做个记录,下来再慢慢研究。或者有大神路过看到,还请不吝赐教!

for (int i = 0;i < dataLists.size();i++) { DataList.DataDTO 数据 = dataLists.get(i).getData(); List<DataList.DataDTO.ComponentsDTO> components = data.getComponents(); for (int j = 0;j < components.size();j++) { 字符串名称 = components.get(j).getName(); 类型 = components.get(j).getType(); Log.i(“组件:”, “” + 类型); setStyle = components.get(j).getSetStyle(); Log.i(“组件:”, 名称); //1文本组件 2图片组件 3视频组件 4天气 5日期 6线路 if (type == 1) { int 高度 = setStyle.getHeight(); int width = setStyle.getWidth(); int left = setStyle.getLeft(); int top = setStyle.getTop(); int marqueeWidth = Integer.parseInt(String.valueOf(width)); int marqueeHeight = Integer.parseInt(String.valueOf(height)); int marqueeTop = Integer.parseInt(String.valueOf(top)); Log.i(“aaaaaaa”, “文本组件marqueeTop:” + marqueeTop); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvMarquee.getLayoutParams(); params.width = dip2px(MainActivity.this, marqueeWidth); params.height = dip2px(MainActivity.this, marqueeHeight); params.leftMargin = dip2px(MainActivity.this, 左);// 设置左边距 params.topMargin = dip2px(MainActivity.this, marqueeTop);// 设置上边距 Log.i(“aaaaaaa”, “文本组件:” + type); } else if (type == 2) { int 高度 = setStyle.getHeight(); int width = setStyle.getWidth(); int left = setStyle.getLeft(); int top = setStyle.getTop(); int imageWidth = Integer.parseInt(String.valueOf(width)); int imageHeight = Integer.parseInt(String.valueOf(height)); int imageTop = Integer.parseInt(String.valueOf(top)); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvImage.getLayoutParams(); params.width = dip2px(MainActivity.this, imageWidth); params.height = dip2px(MainActivity.this, imageHeight); params.leftMargin = dip2px(MainActivity.this, left);// 设置左边距 params.topMargin = dip2px(MainActivity.this, imageTop);// 设置上边距 Log.i(“aaaaaaa”, “图片组件:” + type); } else if (type == 3) { int 高度 = setStyle.getHeight(); int width = setStyle.getWidth(); int left = setStyle.getLeft(); int top = setStyle.getTop(); int videoWidth = Integer.parseInt(String.valueOf(width)); int videoHeight = Integer.parseInt(String.valueOf(height)); int videoTop = Integer.parseInt(String.valueOf(top)); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvVideo.getLayoutParams(); params.width = dip2px(MainActivity.this, videoWidth); params.height = dip2px(MainActivity.this, videoHeight); params.leftMargin = dip2px(MainActivity.this, left);// 设置左边距 params.topMargin = dip2px(MainActivity.this, videoTop);// 设置上边距 Log.i(“aaaaaaa”, “视频组件:” + type); } else if (type == 4) { int 高度 = setStyle.getHeight(); int width = setStyle.getWidth(); int left = setStyle.getLeft(); int top = setStyle.getTop(); int weatherWidth = Integer.parseInt(String.valueOf(width)); int weatherHeight = Integer.parseInt(String.valueOf(height)); int weatherTop = Integer.parseInt(String.valueOf(top)); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvWeather.getLayoutParams(); params.width = dip2px(MainActivity.this, weatherWidth); params.height = dip2px(MainActivity.this, weatherHeight); params.leftMargin = dip2px(MainActivity.this, left);// 设置左边距 params.topMargin = dip2px(MainActivity.this, weatherTop);// 设置上边距 Log.i(“aaaaaaa”, “天气组件:” + type); } else if (type == 5) { int 高度 = setStyle.getHeight(); int width = setStyle.getWidth(); int left = setStyle.getLeft(); int top = setStyle.getTop(); int dateWidth = Integer.parseInt(String.valueOf(width)); int dateHeight = Integer.parseInt(String.valueOf(height)); int dateTop = Integer.parseInt(String.valueOf(top)); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvDate.getLayoutParams(); params.width = dip2px(MainActivity.this, dateWidth); params.height = dip2px(MainActivity.this, dateHeight); params.leftMargin = dip2px(MainActivity.this, left);// 设置左边距 params.topMargin = dip2px(MainActivity.this, dateTop);// 设置上边距 Log.i(“aaaaaaa”, “日期组件:” + type); } else if (type == 6) { int 高度 = setStyle.getHeight(); int width = setStyle.getWidth(); int left = setStyle.getLeft(); int top = setStyle.getTop(); int listWidth = Integer.parseInt(String.valueOf(width)); int listHeight = Integer.parseInt(String.valueOf(height)); int listTop = Integer.parseInt(String.valueOf(top)); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvList.getLayoutParams(); params.width = dip2px(MainActivity.this, listWidth); params.height = dip2px(MainActivity.this, listHeight); params.leftMargin = dip2px(MainActivity.this, left);// 设置左边距 params.topMargin = dip2px(MainActivity.this, listTop);// 设置上边距 Log.i(“aaaaaaa”, “线路组件:” + type); } } } <?xml 版本=“1.0” encoding=“utf-8”?> <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android” 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” android:orientation=“vertical” tools:context=“ 的MainActivity”> <TextView android:id=“@+id/文本” android:layout_width=“wrap_content” 安卓:layout_height=“wrap_content” android:text=“1.0” android:textSize=“30sp” 安卓:layout_gravity=“center” android:visibility=“gone” /> <TextView android:id=“@+id/tv_marquee” android:layout_width=“match_parent” 安卓:layout_height=“50dp” android:background=“@color/黑色” android:text=“跑马灯” android:textColor=“@color/白色” android:textSize=“30sp” android:gravity=“center” /> <TextView android:id=“@+id/tv_video” android:layout_width=“match_parent” 安卓:layout_height=“260dp” android:background=“@color/select_color” android:text=“视频” android:gravity=“center” android:textColor=“@color/白色” android:textSize=“30sp” /> <LinearLayout android:layout_width=“match_parent” 安卓:layout_height=“wrap_content” android:orientation=“horizontal” <TextView android:id=“@+id/tv_date” 安卓:layout_width=“0dp” 安卓:layout_height=“50dp” 安卓:layout_weight=“1” android:background=“@color/common_botton_bar_blue” android:text=“时间日期” android:textColor=“@color/白色” android:gravity=“center” android:textSize=“30sp” /> <TextView android:id=“@+id/tv_weather” 安卓:layout_width=“0dp” 安卓:layout_height=“50dp” android:background=“@color/time_bg” 安卓:layout_weight=“1” android:text=“天气” android:textColor=“@color/白色” android:gravity=“center” android:textSize=“30sp” /> </LinearLayout> <TextView android:id=“@+id/tv_list” android:layout_width=“match_parent” android:layout_height=“match_parent” android:background=“@color/colorPrimary” android:text=“列表” android:textColor=“@color/白色” android:gravity=“center” android:textSize=“30sp” /> </LinearLayout>这段代码根据获取的值实现所有控件可以随便调换位置,怎么修改
06-29
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package android.widget; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.view.ViewDebug.ExportedProperty; public class TableRow extends LinearLayout { public TableRow(Context context) { super((Context)null); throw new RuntimeException("Stub!"); } public TableRow(Context context, AttributeSet attrs) { super((Context)null); throw new RuntimeException("Stub!"); } public void setOnHierarchyChangeListener(ViewGroup.OnHierarchyChangeListener listener) { throw new RuntimeException("Stub!"); } protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { throw new RuntimeException("Stub!"); } protected void onLayout(boolean changed, int l, int t, int r, int b) { throw new RuntimeException("Stub!"); } public View getVirtualChildAt(int i) { throw new RuntimeException("Stub!"); } public int getVirtualChildCount() { throw new RuntimeException("Stub!"); } public LayoutParams generateLayoutParams(AttributeSet attrs) { throw new RuntimeException("Stub!"); } protected LinearLayout.LayoutParams generateDefaultLayoutParams() { throw new RuntimeException("Stub!"); } protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { throw new RuntimeException("Stub!"); } protected LinearLayout.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { throw new RuntimeException("Stub!"); } public CharSequence getAccessibilityClassName() { throw new RuntimeException("Stub!"); } public static class LayoutParams extends LinearLayout.LayoutParams { @ExportedProperty( category = "layout" ) public int column; @ExportedProperty( category = "layout" ) public int span; public LayoutParams(Context c, AttributeSet attrs) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams(int w, int h) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams(int w, int h, float initWeight) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams() { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams(int column) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams(ViewGroup.LayoutParams p) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } public LayoutParams(ViewGroup.MarginLayoutParams source) { super((ViewGroup.LayoutParams)null); throw new RuntimeException("Stub!"); } protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) { throw new RuntimeException("Stub!"); } } } 没有setMinWidth
06-07
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值