众所周知写Android程序的页面布局是通过activity绑定xml文件中事先定义好的文件来实现的,这种实现方式叫做静态布局。但有时无法在程序运行前就决定好页面的布局,或者是控件的属性和数量本身要求不固定的,这种时候就要在程序中定义组件,我们称之为动态布局。
下面为大家介绍一下动态添加布局和控件的方法。
首先创建一个测试用的activity,xml文件下我们只添加一个空的LinearLayout,因为要往这里面添加组件,所以我们还需要给这个Layout添加一个id,就叫root_layout好了。
<LinearLayout 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"
android:id="@+id/root_layout"
android:orientation="vertical"
tools:context=".TestDynamicAddViewActivity">
LinearLayout>
然后在activity创建一个方法,动态添加View,取名就叫dynamic_add好了。
首先要做的是在方法中获取到root_layout对象的引用
protected void dynamic_add(){
LinearLayout rootLayout = (LinearLayout)findViewById(R.id.root_layout);
}
接着我们就要新建各个组件的对象了。layout和widgets的区别不大,在这里一并讲解。
RelativeLayout Layout = new LinearLayout(this);
Button button = new Button(this);
TextView textView = new TextView(this);
可以看出,新建一个组件的方法很简单,new出来就行了。
理论上来讲我们现在就可以把这些组件添加进root_layout了,也确实可以,但是如果对这些组件还有进一步的要求呢?
比如我的textView和button要显示一行我指定的文字,比如我要给他们加id,比如要设定他们的宽度和高度,甚至是一些更复杂的属性。
现在我们假设我们要将button和textView添加到relativeLayout中,并且让button左对齐,让textView右对齐,让button显示文字button,让textView显示Some text,然后将relativeLayout添加到root_Layout里,并设置其大小为宽match_parent,高为wrap_content。
回忆一下用xml文件静态布局时我们是如何实现这些功能的。
<LinearLayout 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"
android:id="@+id/root_layout"
android:orientation="vertical"
tools:context=".TestDynamicAddViewActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:id="@+id/button"
android:layout_toEndOf="@+id/textView"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Text"
android:layout_alignParentRight="true"
android:id="@+id/textView" />
RelativeLayout>
LinearLayout>
如果你有过认真分析,你会发现,每个控件的属性都是一条条“规则”决定的,如果再细分一下,你会发现,这些规则分为父布局的规则和控件自身的规则。
例如,在Button控件中我们定义了android:layout_alignParentLeft=”true”这条规则,而这条规则其实是Button的父布局RelativeLayout的规则。而android:text=”Button”则显而易见是本身的规则。另外,设定宽高也是父布局的规则。
那在动态布局中呢?
对于控件本身的规则,只需要调用控件对象的方法就可以方便地调整了。
下面的新加入的代码就展示了如何动态添加本身规则
button.setText("button");
textView.setText("Some text");
那么父布局的规则呢?我们显然不可能在每一个子控件中都添加所有可能的父布局的规则。在Android中,这是通过父布局的LayoutParams对象来实现的。
现在我们要新建三个LayoutParams对象,分别对应RelativeLayout,Button,和TextView的父布局规则。
LinearLayout.LayoutParams relativeLayout_parent_params
= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams button_parent_params
= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams text_parent_params
= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
button_parent_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
text_parent_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
因为relativeLayout的父布局是LinearLayout,所以调用的是LinearLayout.LayoutParams,下面两个同理。创建一个LayoutParams对象时,要在构造方法输入宽高的参数。至于其他的父布局规则,用addRule方法就行了,xml中对应的规则属性在父布局的LayoutParams类中都有对应的常量。
最后一步,就是将其添加进布局了。
relativerLayout.addView(button,button_parent_params);
relativerLayout.addView(textView,text_parent_params);
rootLayout.addView(relativerLayout,relativeLayout_parent_params);
很简单,只要调用父布局的addView方法,就可以将控件和规则传进去了。
完成后的dynamic_add方法
protected void dynamic_add(){
LinearLayout rootLayout = (LinearLayout)findViewById(R.id.root_layout);
RelativeLayout relativerLayout = new RelativeLayout(this);
Button button = new Button(this);
TextView textView = new TextView(this);
button.setText("button");
textView.setText("Some text");
LinearLayout.LayoutParams relativeLayout_parent_params
= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams button_parent_params
= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams text_parent_params
= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
button_parent_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
text_parent_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relativerLayout.addView(button,button_parent_params);
relativerLayout.addView(textView,text_parent_params);
rootLayout.addView(relativerLayout,relativeLayout_parent_params);
}