Android学习笔记11:线性布局管理器LinearLayout

    接上文
    和Java GUI部分的概念类似,布局管理器用于界面的布局操作,并装载视图组件。在前面的程序中,我们最先涉及到的就是线性布局管理器,对它也有了一定的了解。
    首先,我们来看看线性布局管理器的文档:

java.lang.Object
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ android.widget.LinearLayout

    因为布局管理器也是视图组件,所以都继承自View类,之前的所有示例程序都是使用的线性布局管理器了,对它应该是最熟悉的。下面新建一个项目来看:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
</LinearLayout>

    每个新建的默认项目都会创建一个main.xml文件,其中就是一个线性布局管理器。这里面的xmlns是XML文档的命名空间,这里就是android,那么以android:开头的属性都是该命名空间下的。首先来看layout_width,设置布局管理器的宽度,取值有match_parent,fill_parent和wrap_content,对于布局管理器来说,我们一般使用fill_parent来表示填充整个屏幕宽度。同理,layout_height表示的是布局管理器的高度,我们同样使用fill_parent来表示填充屏幕的高度。orientation表示该布局管理器中组件的排列方式,取值有horizontal和vertical,也就是水平和竖直排列。
下面在这个线性布局管理器中创建几个组件:
<TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sarin" />
    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="辽宁省大连市高新园区七贤岭汇贤园七号" />
    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="腾飞软件园二号楼" />

    我们设置的布局管理器为水平排列组件,而在这三个组件中我们设置长和宽都是包裹内容,那么运行程序,我们看到如下效果:

    显示的内容挤到了一起,而且text2显示不下折行了,但是text3就完全就完全不见了,那么我们把屏幕切到横屏模式下:

    这次没有问题,都显示出来了。也就达到了我们代码中设置的效果。从前面的文档中我们可以看到LinearLayout是View类的子类,那么我们也就可以在Activity程序中进行动态控制,这在之前的示例中也有涉及,这里我们再进一步来研究。
    要在程序代码中控制布局,就要设置布局参数,而在XML中我们可以通过属性来直接定义,但在程序中就涉及到了布局参数类LinearLayout.LayoutParams,我们来看下它的文档:

    这是一个静态类,它的继承结构为:
java.lang.Object
   ↳ android.view.ViewGroup.LayoutParams
   ↳ android.view.ViewGroup.MarginLayoutParams
   ↳ android.widget.LinearLayout.LayoutParams

    下面来看一下代码:
package org.ourpioneer;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class LinearLayoutDemoActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LinearLayout layout = new LinearLayout(this);// 定义线性布局管理器
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.FILL_PARENT);// 定义布局管理器的参数
		layout.setOrientation(LinearLayout.VERTICAL);// 定义组件的排列方式
		LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);// 定义组件的布局参数
		TextView text = new TextView(this);
		text.setLayoutParams(textParams);// 设置组件的参数
		text.setText("Sarin");// 设置显示文字
		layout.addView(text, textParams);// 增加组件
		super.setContentView(layout, params);// 设置布局管理器
	}
}

    LinearLayout的构造方法接收Context类型的参数,那么这里毫无疑问是this,下面就是设置布局参数,需要注意的是FILL_PARENT等参数都是ViewGroup.LayoutParams类中的属性。之后我们创建了一个文本组件,并为文本组件设置它的布局管理参数。最后将文本显示组件添加到布局管理器中,最后设置Activity程序使用的布局管理器,那么我们运行程序,得到如下效果:

    说明我们的代码是没有问题的,我们得到了预期的效果。
    接下文

猜你喜欢

转载自sarin.iteye.com/blog/1689169