xml布局和动态布局结合使用
2012-05-09 09:44
155 查看
1. 新建工程
2. 新建布局 userview.xml, 布局中上面部分是一个textview, 下部分是一个button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
3. 新建View--MyView, 首先加载了userview布局,然后横向部分新增加了一个button按钮
public class MyView extends LinearLayout {
private View userview;
private Button anotherButton;
public MyView(Context context, AttributeSet attrs) {
super(context);
// TODO Auto-generated constructor stub
setOrientation(LinearLayout.HORIZONTAL);
userview = inflate(context, R.layout.userview, null);//这里的第三个参数应该为null
anotherButton = new Button(context);
anotherButton.setText("another");
addView(userview);
addView(anotherButton);
}
}
4. MainActivity
public class MainActivity extends Activity {
private MyView myView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myView = new MyView(this, null);
setContentView(myView);
}
}
2. 新建布局 userview.xml, 布局中上面部分是一个textview, 下部分是一个button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
3. 新建View--MyView, 首先加载了userview布局,然后横向部分新增加了一个button按钮
public class MyView extends LinearLayout {
private View userview;
private Button anotherButton;
public MyView(Context context, AttributeSet attrs) {
super(context);
// TODO Auto-generated constructor stub
setOrientation(LinearLayout.HORIZONTAL);
userview = inflate(context, R.layout.userview, null);//这里的第三个参数应该为null
anotherButton = new Button(context);
anotherButton.setText("another");
addView(userview);
addView(anotherButton);
}
}
4. MainActivity
public class MainActivity extends Activity {
private MyView myView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myView = new MyView(this, null);
setContentView(myView);
}
}
相关文章推荐
- 【android动态布局】之【ListView动态加载数据模板(使用xml布局)】
- ListView动态加载数据模板(使用xml布局)
- 【android动态布局】之【ListView动态加载数据模板(使用xml布局)】
- 【android动态布局】之【ListView动态加载数据模板(使用xml布局)】
- 一种使用Xml对Android界面进行动态布局的方法
- Android 中使用代码动态网格布局
- android:weight属性的使用——android开发之xml布局文件
- 使用Fragment实现动态布局
- Android-使用XML布局文件实现游戏的开始界面
- Android 动态布局实践 - 使用FrameLayout
- android使用代码布局LInearLayout达不到xml的效果(自问自答)
- Spring-hibernate使用全xml配置结合方式搭建开发
- android 使用xml布局自己的对话框
- RSA - 与DES结合使用实现动态加密
- android使用xml布局文件设计提示对话框
- 使用WIFIMAN.DLL动态库结合Delphi实现Windows无线管理器。
- Android中xml布局文件中使用include引入布局进行复用(解决使用include布局重叠,颜色设置无效问题)
- android: 静态XML和动态加载XML混合使用,以及重写Layout控件
- Spring学习-13:Spring的XML和注解的结合使用
- 混合使用XML布局文件和代码控制视图