<LinearLayoutandroid:id="@+id/ll_indicator"android:orientation="horizontal"android:layout_height="@dimen/size40" //40dpandroid:layout_width="match_parent"><!-- 存放三个indicator --></LinearLayout>


<?xml version="1.0" encoding="utf-8"?><!-- 某个indicator ,如img+今日提醒--><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/item_indicator"android:layout_height="match_parent"android:layout_width="match_parent"android:gravity="center"android:orientation="vertical"><ImageViewandroid:id="@+id/img_tab1_indicator"android:layout_width="wrap_content"android:layout_height="0dp"android:layout_weight="1"/><TextViewandroid:id="@+id/tv_tab1_indicator"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="@dimen/textSize13" //13sp/></LinearLayout>

第一个子view会占据了父容器全部空间,另外2个子View在右边去了,看不到了。
!
实际上它长这样的:

LinearLayout ll_indicator = (LinearLayout) rootView.findViewById(R.id.ll_indicator);for(int i=0;i<mTextArray.length;i++){//曾经一直以为这个下面返回的v是 layout布局文件代表的view,但是官方解释是:The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file. 解释是说放回的是root view,这个root view就是视图层中外面的容器view,加入下面第三个参数root参数给的是null,那么返回的就是layout代表的对象ViewGroup v =(ViewGroup) View.inflate(mActivity, R.layout.pre_tab1_indicator, ll_indicator);Log.i(TAG, "是否为同一对象:"+(ll_indicator == v)); //打印为true}

应该是我们猜测的"应该的样子(图二)",第一个子view占满父容器,二、三子view去到了屏幕的右边。。。看不到的地方了吧。
,实际上它长这样的
:

// Temp is the root view that was found in the xmlView temp;if (TAG_1995.equals(name)) {temp = new BlinkLayout(mContext, attrs);} else {temp = createViewFromTag(root, name, attrs);}ViewGroup.LayoutParams params = null;if (root != null) {if (DEBUG) {System.out.println("Creating params from root: " +root);}// Create layout params that match root, if suppliedparams = root.generateLayoutParams(attrs);if (!attachToRoot) {// Set the layout params for temp if we are not// attaching. (If we are, we use addView, below)temp.setLayoutParams(params);}}if (DEBUG) {System.out.println("-----> start inflating children");}// Inflate all children under temprInflate(parser, temp, attrs, true);if (DEBUG) {System.out.println("-----> done inflating children");}// We are supposed to attach all the views we found (int temp)// to root. Do that now.if (root != null && attachToRoot) {//如果调用View.inflate(mActivity, R.layout.pre_tab1_indicator, null),那么root!=null那么attachToRoot就为true,自己可以看源码。root.addView(temp, params); //这里会给子 子view设置 params,也就是写在了xml中的layout_height等等等属性。。。}// Decide whether to return the root that was passed in or the// top view found in xml.if (root == null || !attachToRoot) {result = temp; //这个temp 或这个result就是 需要inflate 后的 root View}
//这里attachToRoot
就是当你给的View.inflate(mActivity, R.layout.pre_tab1_indicator, null); 第三个参数不为null时它的 attachToRoot
是true, 那么下面 如果给root参数为null,这个构建的对象view对象就没有LayoutParams,
如果一个view没有LayoutParams被add进父容器时,系统会给它设置一个默认的,这个就是wrap_content高和宽。下面看addView的源码:
public void addView(View child, int index) {LayoutParams params = child.getLayoutParams();if (params == null) {params = generateDefaultLayoutParams(); //这里就是 new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);if (params == null) {throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");}}addView(child, index, params);}
ll_indicator = (LinearLayout) rootView.findViewById(R.id.ll_indicator);for(int i=0;i<mTextArray.length;i++){ViewGroup v =(ViewGroup) View.inflate(mActivity, R.layout.pre_tab1_indicator, ll_indicator);Log.e(TAG, "layout文件是否等于最外面的view?:"+(v == v.findViewById(R.id.item_indicator)));Log.e(TAG, (ll_indicator==v)+"?同一个对象么?");// LayoutParams params = v.getLayoutParams();// Log.e(TAG, "当前的params:"+params+"---"); //得到null,所以系统给默认布局参数 wrap_content// v.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1));v.setTag(i); //绑定索引号。ImageView img_view = (ImageView) v.findViewById(R.id.img_tab1_indicator); //看到没每次从v中获取imgview,这个v始终都是同一个。所以TextView tv = (TextView) v.findViewById(R.id.tv_tab1_indicator); //造成了第三个会覆盖imgview对图片资源的设置img_view.setImageResource(i==0?mImgArray[1][i]:mImgArray[0][i]);tv.setText(mTextArray[i]);tv.setTextColor(getResources().getColor(i==0?R.color.pregnancy_tv_color_common:android.R.color.darker_gray));// ll_indicator.addView(v);indicatorItem[i] = v;imgIndicator[i] = img_view;tvIndicator[i] = tv;v.setOnClickListener(this);v.setOnTouchListener(new MyTouchListener(i));}



原文:http://blog.csdn.net/joychine/article/details/40872593