android: 静态XML和动态加载XML混合使用,以及重写Layout控件 收藏
近期对android里面控件修改做了很多实验,由于公司需求很多,不得不重写很多控件。程序目标无非是:高效、轻巧、清晰、标准化





完成动态加载Layout有两种方法,依据个人喜好进行选择:

方法1:静态主Layout动态加载静态子Layout

首先构建子Layout:main2

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<!--布局可以任意定义,此处拿线性布局举例,里面有2个按钮元素-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menubar"
android:background="@drawable/menubar"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--按钮1-->
<ImageButton android:id="@+id/button1"
android:src="@drawable/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></ImageButton>
<!--按钮2-->
<ImageButton android:id="@+id/button2"
android:src="@drawable/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></ImageButton>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<!--布局可以任意定义,此处拿线性布局举例,里面有2个按钮元素-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menubar"
android:background="@drawable/menubar"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--按钮1-->
<ImageButton android:id="@+id/button1"
android:src="@drawable/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></ImageButton>
<!--按钮2-->
<ImageButton android:id="@+id/button2"
android:src="@drawable/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></ImageButton>
</LinearLayout>

然后构建主Layout:main

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">
<!--主Layout要给子Layout设置一个容器box,可以在此指定容器的位置,这段是关键部分-->
<LinearLayout android:id="@+id/box"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">
<!--主Layout要给子Layout设置一个容器box,可以在此指定容器的位置,这段是关键部分-->
<LinearLayout android:id="@+id/box"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
</LinearLayout>
</RelativeLayout>

最后在程序中加载子layout:

view plaincopy to clipboardprint?
public class BackgroundTest extends Activity {
/** Called when the activity is first created. */
// 子Layout要以view的形式加入到主Layout中
private View mBarView;
// 主Layout的容器加载子Layout的View
private LinearLayout mLinearLayout;
//给出关键内容
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 显示主Layout
setContentView(R.layout.main);
// 加载子Layout
mBarView = View.inflate(this, R.layout.main2, null);
// 找到容器
mLinearLayout = (LinearLayout)findViewById(R.id.box);
// 加上View 结束
mLinearLayout.addView(mBarView);
}
public class BackgroundTest extends Activity {
/** Called when the activity is first created. */
// 子Layout要以view的形式加入到主Layout中
private View mBarView;
// 主Layout的容器加载子Layout的View
private LinearLayout mLinearLayout;
//给出关键内容
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 显示主Layout
setContentView(R.layout.main);
// 加载子Layout
mBarView = View.inflate(this, R.layout.main2, null);
// 找到容器
mLinearLayout = (LinearLayout)findViewById(R.id.box);
// 加上View 结束
mLinearLayout.addView(mBarView);
}

方法2:静态主Layout动态加载动态的Layout

首先构造你自己的子Layout和上面一样;

然后构建你自定义的Layout类:

view plaincopy to clipboardprint?
public class MenuLandscapeLinearLayout extends LinearLayout{
// 构造函数
public MenuLandscapeLinearLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
//加载需要的属性,加载方法一的子Layout
((Activity) getContext()).getLayoutInflater().inflate(R.layout.main2, this);
//在此你可以封装很多方法
}
}
public class MenuLandscapeLinearLayout extends LinearLayout{
// 构造函数
public MenuLandscapeLinearLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
//加载需要的属性,加载方法一的子Layout
((Activity) getContext()).getLayoutInflater().inflate(R.layout.main2, this);
//在此你可以封装很多方法
}
}

最后在程序中动态实例化并加载即可:

view plaincopy to clipboardprint?
public class BackgroundTest extends Activity {
/** Called when the activity is first created. */
private LinearLayout mLinearLayout;
//声明一个子Layout View对象
private MenuLandscapeLinearLayout mMenuLandscapeLinearLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 加载主Layout
setContentView(R.layout.main);
// 找到容器
mLinearLayout = (LinearLayout)findViewById(R.id.box);
// 实例化一个子View
mMenuLandscapeLinearLayout=new MenuLandscapeLinearLayout(this);
// 添加到容器
mLinearLayout.addView(mMenuLandscapeLinearLayout);
}
}
public class BackgroundTest extends Activity {
/** Called when the activity is first created. */
private LinearLayout mLinearLayout;
//声明一个子Layout View对象
private MenuLandscapeLinearLayout mMenuLandscapeLinearLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 加载主Layout
setContentView(R.layout.main);
// 找到容器
mLinearLayout = (LinearLayout)findViewById(R.id.box);
// 实例化一个子View
mMenuLandscapeLinearLayout=new MenuLandscapeLinearLayout(this);
// 添加到容器
mLinearLayout.addView(mMenuLandscapeLinearLayout);
}
}


至此,完成了动态加载子Layout的两种形式,里面可思考的很多,比如封装常用事件、资源,从而节省代码、节省资源;

更多相关文章

  1. xamarin Android(安卓)activity生命周期详解
  2. Android(安卓)人脸识别+人脸匹配(OpenCV+JavaCV)
  3. Android中AsyncTask的简单用法
  4. Android中线程形态AsyncTask、HandlerThread 和 IntentService简
  5. Cocos2d-x shareSDK
  6. Android(安卓)webview
  7. [Android(安卓)Studio系列(五)] Android(安卓)Studio手动配置Gra
  8. 浅谈Java中Collections.sort对List排序的两种方法
  9. Python list sort方法的具体使用

随机推荐

  1. Android(安卓)真实 简历
  2. 更换android的初始化图片
  3. sdk2.2. 没发现有google map api 离线下
  4. Android学习札记20:ScaleGestureDetector
  5. Android中的骨架加载预览(Skeleton),Recycle
  6. java.lang.Class Cast Exception: androi
  7. AndroidStudio常见问题
  8. 优化 Android(安卓)Studio 启动、编译、
  9. Android(安卓)Studio AIDL实现
  10. 有空待研究的几篇不错的surfaceflinger文