布局其實可以說就是一個盒子,一個裝着其他元件的盒子。
所謂線性布局就是元件在水準方向或者豎直方向依次排列的布局
<LinearLayout //線性布局管理器 xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" //所有元件采用垂直方式由上向下排列 android:layout_width="fill_parent"// 此布局管理器将填充整個螢幕寬度 android:layout_height="fill_parent"> // 此布局管理器将填充整個螢幕高度
…………………………………………………………毫無美感的分割線…………………………………………………………
下面我們進行布局管理的配置
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >//垂直擺放元件,如果設定為horizontal則為水準放置
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是第一行" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="我是第二行" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是第三行" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是第四行" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="我是第五行" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是第六行" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是第七行" />
</RadioGroup>
</LinearLayout>
JAVA檔案不用設定
package com.example.linearlayout13;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linearlayout);
}
}
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICdzFWRoRXdvN1LclHdpZXYyd2LcBzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CX90zZONzaq5EeZpXT4FEVkZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39DM1cTMzczM5EDNxQDM0EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
大家可以看到,上面的元件都是水準放置,是因為:
大家可以在下面自己試試。
當然也可以在JAVA程式中進行設定。
…………………………………………………………毫無美感的分割線…………………………………………………………
LinearLayout元件類的繼承結構: java.lang.Object ↳ android.view.View ↳ android.view.ViewGroup ↳ android.widget.LinearLayout
No. | 方法及常量 | 類型 | 描述 |
1 | public static final int HORIZONTAL | 常量 | 設定水準對齊 |
2 | public static final int VERTICAL | 常量 | 設定垂直對齊 |
3 | public LinearLayout(Context context) | 構造 | 建立LinearLayout類的對象 |
4 | public void addView(View child, ViewGroup.LayoutParams params) | 普通 | 增加元件并且指定布局參數 |
5 | public void addView(View child) | 普通 | 增加元件 |
6 | protected void onDraw(Canvas canvas) | 普通 | 用于圖形繪制的方法 |
7 | public void setOrientation(int orientation) | 普通 | 設定對齊方式 |
LinearLayout.LayoutParams類提供了以下一個構造方法: public LinearLayout.LayoutParams (int width, int height) 常用布局參數: public static final int FILL_PARENT public static final int WRAP_CONTENT
配置檔案預設
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
JAVA檔案配置
package com.example.linearlayout13;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);//建立現行布局
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);//設定現行布局的配置
layout.setOrientation(LinearLayout.VERTICAL);//設定現行布局的對齊方式
LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);//設定文本的對齊方式
for (int i = 0; i < 5; i++) {
TextView text = new TextView(this);//執行個體化TextView
text.setLayoutParams(textParams);//設定預設配置方式
text.setText("第" + i + "行");
text.setTextSize(20);//設定文本大小
layout.addView(text, textParams);//将文本元件添加到線性布局
}
super.addContentView(layout, layoutParams);//設定線性布局
}
}
大家也可以根據自己想法設定想要的元件,我比較懶就用循環輸出了。
下節預報:
架構布局管理器:FrameLayout