80,360
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
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);
}
}
<?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>
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
}
<?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>