android使用代码进行布局
2011-12-22 16:25
465 查看
大家都知道,在使用eclipse进行android开发的时候,我们可以通过布局文件(.xml文件)对程序的界面进行布局。同时肯定能想到,通过代码也可以实现同样的功能。只是平时会用得少,且比较麻烦,但在某些情况下还是挺有用的。下面就来说明如何通过代码进行:
我依然用到了两个.xml文件,left.xml、right.xml, 内容如下left.xml:
right.xml:
源文件代码实现如下:
本文出自 “lilingshui” 博客,请务必保留此出处http://qsjming.blog.51cto.com/1159640/748664
我依然用到了两个.xml文件,left.xml、right.xml, 内容如下left.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/left" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/view1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/blue" android:text="@string/left_view1" /> <TextView android:id="@+id/view2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/view1" android:background="@drawable/yellow" android:text="@string/left_view2" /> </RelativeLayout>
right.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/right" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/right_view1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/yellow" android:gravity="center" android:text="@string/right_view1" /> <TextView android:id="@+id/right_view2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/right_view1" android:background="@drawable/blue" android:gravity="center" android:text="@string/right_view2" /> </RelativeLayout>
源文件代码实现如下:
import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.LinearLayout; import android.widget.RelativeLayout; public class ActivityLayout extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // 通过代码创建一个linearlayout并将它设为activity的内容 LinearLayout layoutmain = new LinearLayout(this); layoutmain.setOrientation(LinearLayout.HORIZONTAL); setContentView(layoutmain); // 获得具备XML解析功能的LayoutInflater LayoutInflater inflate = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); //解析left.xml文件构造RelativeLayout RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate( R.layout.left, null); //解析right.xml文件构造RelativeLayout RelativeLayout layoutRight = (RelativeLayout) inflate.inflate( R.layout.right, null); //设置相关的控件在layoutmain中的摆放参数 RelativeLayout.LayoutParams leftParam = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); RelativeLayout.LayoutParams rightParam = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); //将空间添加到layoutmain中 layoutmain.addView(layoutLeft, leftParam); layoutmain.addView(layoutRight, rightParam); } }
本文出自 “lilingshui” 博客,请务必保留此出处http://qsjming.blog.51cto.com/1159640/748664
相关文章推荐
- Android中使用Java代码对界面进行布局
- android 使用代码和xml配合进行布局
- Android使用ProGuard进行代码混淆
- 关于Android使用proguard进行代码混淆
- Jenkins中使用火线进行Android静态代码扫描
- Android 解决ListView在使用多个布局的同时使用convertView进行缓存时导致ListView下面有空白的问题
- Android ViewPager使用详解,加载几个简单布局案例+代码
- [Android]使用Emma进行单体测试代码覆盖率分析
- Android安卓使用iosocket进行Socket通信简单封装优雅写代码
- Masonry -- 使用纯代码进行iOS应用的autolayout自适应布局
- 一种使用Xml对Android界面进行动态布局的方法
- xib中使用Autolayout进行布局,在代码里面如何进行修改布局
- Masonry -- 使用纯代码进行iOS应用的autolayout自适应布局
- android TextView EditTextView一些技巧使用 (视图代码布局)
- 关于Android使用proguard进行代码混淆
- 使用proguard对Android Library中代码进行混淆
- Android利用dimens.xml进行适配,使用代码生成不同的dimens.xml文件
- 关于Android使用proguard进行代码混淆
- Android中使用Json和Xml与服务器进行通信,使用代码发送Get和Post请求,http请求辅助类
- androidUI第三部分---1.3.2不使用xml文件手工代码实现布局