直接上代码吧:一直在纠结求各位大神点破:
一、这是MainActivity:
public class MainActivity extends Activity {
private MyRelativeLayout mRL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mRL=(MyRelativeLayout)LayoutInflater.from(this).inflate(R.layout.activity_main, null);
setContentView(R.layout.activity_main);
}
}
二、这是MyActivity要显示的MyRelativeLayout
public class MyRelativeLayout extends RelativeLayout {
MyLinearLayout mLL;
private Context mContext; public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext=context;

// TODO Auto-generated constructor stub
}

@Override
public void onFinishInflate(){
super.onFinishInflate();
init();

}
public void init(){
mLL=(MyLinearLayout)this.inflate(mContext,R.layout.relative,null);
}}
三、这是MyRelativeLayout对应的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<com.example.viewtest.MyRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
  
    <com.example.viewtest.MyLinearLayout
        android:id="@+id/layout"
        android:layout_width="200px"
        android:layout_height="200px"
        android:background="#ff0000">
        
    </com.example.viewtest.MyLinearLayout>
   
       
    <ImageView
        android:layout_below="@id/layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     />
</com.example.viewtest.MyRelativeLayout>
四、这是嵌套的MyLinearLayout:
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
}
五、这个嵌套MyLinear对应的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<com.example.viewtest.MyLinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" /></com.example.viewtest.MyLinearLayout>问题是:
现在不现实嵌套的MyLinearLayout中的TextView。

解决方案 »

  1.   

    你的activity_main布局是:
    <?xml version="1.0" encoding="utf-8"?>
    <com.example.viewtest.MyRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
      
        <com.example.viewtest.MyLinearLayout
            android:id="@+id/layout"
            android:layout_width="200px"
            android:layout_height="200px"
            android:background="#ff0000">
            
        </com.example.viewtest.MyLinearLayout>
       
           
        <ImageView
            android:layout_below="@id/layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
         />
    </com.example.viewtest.MyRelativeLayout>你的relative布局是:
    <?xml version="1.0" encoding="utf-8"?>
    <com.example.viewtest.MyLinearLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >    <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" /></com.example.viewtest.MyLinearLayout>????????????是这样的么
      

  2.   

    你把你MyRelativeLayout类的init方法这样写,你虽然引入了外部布局但是没有添加public void init() {
              mLL = (MyLinearLayout) this.inflate(mContext, R.layout.relative, null);
              addView(mLL);
    }
      

  3.   

    RelativeLayout 中不应该是 include 的吗?
      

  4.   

    你 MyLinearLayout 里面什么东西都没有加啊? 你xml 里面的<com.example.viewtest.MyLinearLayout 只是引用的自定义类,你这个类又没有添加任何子View或者关联layout
      

  5.   

    MyRelativeLayout 里面都infalte 了,MyLinearLayout  为什么不 infalte ?  
      

  6.   

    不是啊 Activity就是一个RelativeLayout,RelativeLayout里套的一个LinearLayout。
      

  7.   


    public class MyRelativeLayout extends RelativeLayout {
        MyLinearLayout mLL;
        private Context mContext;
     
        public MyRelativeLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.mContext=context;
         
            // TODO Auto-generated constructor stub
        }
         
        @Override
        public void onFinishInflate(){
            super.onFinishInflate();
            init();
         
        }
        public void init(){
            mLL=(MyLinearLayout)this.inflate(mContext,R.layout.relative,null);
        }
     
    }标红这行算不算啊
      

  8.   


    我见别人也这么写过啊,但就是显示不出来。刚刚参照2楼做了,显示出来了,但是并不是按照我在RelativeLayout中定义的位置显示的。