안드로이드——레이아웃 관리자 (11)

1. 선형 레이아웃 관리자: LinearLayout

1.1 지식 포인트

(1) 레이아웃 관리자 소개

(2) 라인 레이아웃 관리자 구성;

(3) Activity 프로그램을 통한 라인 레이아웃

1.2 구체적 내용

안드로이드에서는 컴포넌트가 정의되어 있는 한 해당 컴포넌트는 선형 레이아웃 관리자에 배치되어야 하며, 또 다른 부분은 컴포넌트가 일반적으로 이벤트 처리 작업을 수행한다는 것입니다.

 먼저 선형 레이아웃 관리자를 중심으로 설명하겠습니다.

<LinearLayout 
    xmlns:android=http://schemas.android.com/apk/res/android—布局文件包含的是Android的命名空间
    xmlns:tools=http://schemas.android.com/tools—命名空间的工具
    android:layout_width="match_parent" –此布局宽度为全屏
    android:layout_height="match_parent" – 此布局高度全屏
    android:orientation="vertical"> --此布局中组件垂直码放
</LinearLayout>

선형 레이아웃 관리자는 안드로이드에서 가장 일반적으로 사용되는 레이아웃 관리자입니다.

클래스의 통합 관계를 통해 선형 레이아웃 관리자는 특별한 구성 요소이며 이전 구성 요소와 비교하면 다른 구성 요소만 배치할 수 있다는 것을 알 수 있습니다.

 

 

<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:orientation="horizontal" >

    <Button
        android:id="@+id/but1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="爱科技有限公司" />
    <Button
        android:id="@+id/but2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="www.wanczy.com" />
     <Button
        android:id="@+id/but3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="毛栗子" />

</LinearLayout>

 물론 Activity 프로그램을 통해 레이아웃 관리자를 구성할 수도 있으며 이제 더 이상 레이아웃 파일을 사용하지 않습니다.

package com.example.linearlayoutproject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;

public class LinearLayoutActivity extends Activity {
	private LinearLayout layout = null;
	private Button but1 = null;
	private Button but2 = null;
	private Button but3 = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.layout = new LinearLayout(this);
        LinearLayout.LayoutParams layoutParams = 
        		new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
        				LinearLayout.LayoutParams.MATCH_PARENT);//准备布局管理器布局参数
        this.layout.setOrientation(LinearLayout.VERTICAL);//设置为垂直码放
        this.layout.setLayoutParams(layoutParams);//设置布局参数
        this.but1 = new Button(this);
        this.but1.setText("爱科技有限公司");
        this.but2 = new Button(this);
        this.but2.setText("www.aikeji.com");
        this.but3 = new Button(this);
        this.but3.setText("毛栗子");
        LinearLayout.LayoutParams butParams = 
        		new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
        				LinearLayout.LayoutParams.WRAP_CONTENT);//准备按钮布局参数
        this.layout.addView(this.but1,butParams);
        this.layout.addView(this.but2,butParams);
        this.layout.addView(this.but3,butParams);
        super.setContentView(layout);//此Activity程序使用的就是我们定义的布局管理器
    }
}

위 프로그램은 레이아웃 파일을 사용하지 않고 선형 레이아웃을 완성하기 위해 Activity 프로그램을 사용하지만, 일반적으로 개발 시 레이아웃 파일에 레이아웃 관리자를 구성해야 합니다.

1.3 요약

(1) 선형 레이아웃에는 가로와 세로의 두 가지 배열이 있습니다.

(2) 선형 레이아웃은 LinearLayout 클래스를 통해 정의할 수 있으며, 레이아웃 매개변수는 LinearLayout.LayoutParams 클래스를 통해 완성할 수 있습니다.

2. 프레임 레이아웃 관리자: FrameLayout

2.1 지식 포인트

(1) 프레임 레이아웃 관리자의 사용 특성을 마스터합니다.

(2) 레이아웃 관리자의 프레임 레이아웃을 사용하여 정렬할 수 있습니다.

(3) FrameLayout 및 FrameLayout.LayoutParams 클래스를 사용하여 활동 프로그램에서 레이아웃을 동적으로 생성할 수 있습니다.

2.2 구체적 내용

<FrameLayout 
    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">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/android02" />
    <EditText 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入您的姓名"
        />
	<Button 
	    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你点我呀"
	    />
</FrameLayout>

 위 프로그램의 경우 효과를 느낄 수 없습니다.

이제 레이아웃 파일을 사용하지 않고 활동 프로그램에서 직접 레이아웃을 생성할 수 있습니다.

package com.example.framelayoutproject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class FrameLayoutActivity extends Activity {
	private FrameLayout layout = null;
	private ImageView imgView = null;
	private EditText editText = null;
	private Button but = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.layout = new FrameLayout(this);
		FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.MATCH_PARENT,
				FrameLayout.LayoutParams.MATCH_PARENT);// 准备FrameLayout布局参数
		this.layout.setLayoutParams(layoutParams);// 设置FrameLayout布局参数
		this.imgView = new ImageView(this);
		imgView.setImageResource(R.drawable.android02);// 设置图片组件要显示的图片
		this.editText = new EditText(this);
		this.editText.setText("请输入姓名:");
		this.but = new Button(this);
		this.but.setText("你点我呀");
		// 准备组件的布局参数
		FrameLayout.LayoutParams childParams = new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.WRAP_CONTENT,
				FrameLayout.LayoutParams.WRAP_CONTENT);//准备了组件的布局参数
		this.layout.addView(this.imgView,childParams);
		this.layout.addView(this.editText,childParams);
		this.layout.addView(this.but,childParams);
		setContentView(layout);
	}
}

 효과는 여전히 동일하지만 향후 개발에서는 여전히 레이아웃 파일에서 레이아웃을 수행해야 합니다. 활동에는 거의 작성되지 않습니다.

2.3 요약

(1) 프레임 레이아웃은 구성 요소를 사용하여 지정된 영역을 채웁니다.

(2) FrameLayout 및 FrameLayout.LayoutParams 클래스를 사용하여 레이아웃을 수동으로 구성할 수 있습니다.

3. 테이블 레이아웃 관리자: TableLayout

3.1 지식 포인트

(1) 테이블 레이아웃 관리자의 기본 사용법을 숙지하십시오.

(2) TableLayout과 TableRow 간의 작동 관계를 마스터합니다.

(3) 테이블 레이아웃 관리자의 공통 속성의 역할을 마스터합니다.

3.2 구체적 내용

일반적으로 작업을 할 때 데이터의 형식을 지정하기 위해 테이블을 사용하는 경우가 많으며, 이 경우 데이터를 읽는 것이 더 편리하고 아름답다는 장점이 있습니다.

예:

<TableLayout 
    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" >
	<TableRow –表示行
	    android:orientation="horizontal"
	    >
		<EditText 
		    android:layout_width="wrap_content"
    		    android:layout_height="wrap_content" 
		    android:text="请输入检索关键字"
		    />	    
	    <Button
	        android:layout_width="wrap_content"
    		    android:layout_height="wrap_content" 
    		    android:text="检索"
	        />
	</TableRow>
  	<TableRow 
	    android:orientation="horizontal"
	    >
		<TextView 
		    android:layout_width="wrap_content"
    		    android:layout_height="wrap_content" 
		    android:text="请选择编码"
		    />	    
	   <RadioGroup 
	       android:orientation="vertical"
	       android:layout_width="wrap_content"
    	       android:layout_height="wrap_content" 
    	       android:checkedButton="@+id/rb2"—默认选中rb2
	       >
	       <RadioButton 
	           android:id="@+id/rb1"
	           android:layout_width="wrap_content"
    		       android:layout_height="wrap_content" 
    		       android:text="UTF编码"
	           />
	       <RadioButton 
	           android:id="@+id/rb2"
	           android:layout_width="wrap_content"
    		   android:layout_height="wrap_content" 
    		   android:text="GBK编码"
	           />
	   </RadioGroup>
	</TableRow>
</TableLayout>

 위 프로그램은 단순 조판만을 구현하고 있으며, 테이블의 경우 일부 기본 조판 외에 데이터 표시에도 자주 사용됩니다.

<TableLayout 
    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" >
	<TableRow 
	    android:orientation="horizontal"
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content" 
	    >
	    <TextView 
	        android:layout_column="0"—定义列号
	        android:layout_width="wrap_content"
    		    android:layout_height="wrap_content" 
	        android:text="ID"
	        />
	    <TextView 
	        android:layout_column="1"
	        android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
	        android:text="姓名"
	        />
	    <TextView 
	        android:layout_column="2"
	        android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
	        android:text="Email"
	        />
	    <TextView 
	        android:layout_column="3"
	        android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
	        android:text="地址"
	        />
	</TableRow>
	<View –这边是定义一根横线
	    android:layout_height="2px"
	    android:background="#330033"
	    />
	<TableRow 
	    android:orientation="horizontal"
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content" 
	    >
	    <TextView 
	        android:layout_column="0"
	        android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
	        android:text="Wanczy"
	        />
	    <TextView 
	        android:layout_column="1"
	        android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
	        android:text="爱科技"
	        />
	    <TextView 
	        android:layout_column="2"
	        android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
	        android:text="[email protected]"
	        />
	    <TextView 
	        android:layout_column="3"
	        android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
	        android:text="光明大道1号"
	        />
	</TableRow>
	<TableRow 
	    android:orientation="horizontal"
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content" 
	    >
	    <TextView 
	        android:layout_column="0"
	        android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
	        android:text="MLZ"
	        />
	    <TextView 
	        android:layout_column="1"
	        android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
	        android:text="毛栗子"
	        />
	    <TextView 
	        android:layout_column="2"
	        android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
	        android:text="[email protected]"
	        />
	    <TextView 
	        android:layout_column="3"
	        android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
	        android:text="光明大道2号"
	        />
	</TableRow>
</TableLayout>

화면이 표시할 만큼 넓지 않은 경우 이때 텔레스코픽 기둥을 설정할 수 있습니다.

<TableLayout 
    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:shrinkColumns="3">--表示设置第3列为伸缩列

이제 하나의 열을 확장 가능한 열로 설정했지만 여러 열을 설정하려면 어떻게 해야 할까요?

<TableLayout 
    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:shrinkColumns="0,1,2,3">--设置了4个伸缩列

확장 가능한 열 설정 외에도 숨겨진 열도 설정할 수 있습니다.

<TableLayout 
    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:shrinkColumns="3"
    android:collapseColumns="0">--隐藏第0列

테이블의 배경을 설정합니다.

<TableLayout 
    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:shrinkColumns="3"
    android:collapseColumns="0"
    android:background="@drawable/background">--设置背景图片

전체 코드의 관점에서 볼 때 테이블 레이아웃은 매우 유연하지만 Activity 프로그램을 사용하여 테이블 작업을 완료하려는 경우 매우 번거로운 프로세스가 됩니다.

package com.example.tablelayoutproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TableLayoutActivity extends Activity {
	private String titleData[][] = new String[][] {
			{ "ID", "姓名", "EMAIL", "地址" },		// 标题头
			{ "爱科技", "[email protected]","光明大道1号" },
			{"毛栗子", "mlz_fc.com", "光明大道2号" } };// 显示数据
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			TableLayout layout = new TableLayout(this);	// 表格布局
			TableLayout.LayoutParams layoutParam = new TableLayout.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, // 布局管理器宽度为屏幕宽度
				ViewGroup.LayoutParams.FILL_PARENT);// 布局管理器高度为屏幕高度
			layout.setBackgroundResource(R.drawable.background);// 设置背景图片
			for (int x = 0; x < this.titleData.length; x++) {
				TableRow row = new TableRow(this); 	// 定义表格行
				for (int y = 0; y < this.titleData[x].length; y++) {
					TextView text = new TextView(this);// 创建文本组件
					text.setText(this.titleData[x][y]); // 设置文本内容
					row.addView(text, y); 	// 增加组件
				}
				layout.addView(row); 			// 增加表格行
			}
			super.setContentView(layout, layoutParam); 		// 定义组件
		}

}

3.3 요약

(1) 테이블 레이아웃 관리자는 TableRow를 사용하여 테이블 행을 제어합니다.

(2) 테이블 레이아웃의 여러 속성:

        축소 열 정의: android:shrinkColumns="3"

        열을 표시하지 않도록 설정: android:collapseColumns="0,3"

        배경 이미지 추가: android:Background="@drawable/mldn_logo"

(3) Activity 프로그램을 이용하여 테이블 레이아웃을 동적으로 생성할 수도 있습니다.

4. 상대 레이아웃 관리자: RelativeLayout

4.1 지식 포인트

(1) 관련 레이아웃 관리자의 주요 기능과 사용법을 숙지합니다.

(2) Activity 프로그램을 이용하여 컴포넌트를 동적으로 추가할 수 있습니다.

4.2 구체적 내용

상대 레이아웃 관리자는 이전에 언급한 세 가지 유형에 대해 덜 일반적으로 사용되지만, 상대 레이아웃은 구성 요소를 참조로 사용하여 위, 아래, 왼쪽, 오른쪽 또는 반대 방향에 배치한다는 점을 이해해야 합니다. .

<RelativeLayout 
    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" >
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是一个文本显示组件" />
    <Button
        android:id="@+id/but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是一个按钮" 
        android:layout_toRightOf="@+id/text"--放在text组件的右边
        android:layout_below="@+id/text"/>--放在text组件的下边
    <ImageView 
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_toRightOf="@+id/text"—放在text组件的右边
        android:layout_above="@+id/but"—放在but组件的上边
        />
</RelativeLayout>

 위의 프로그램은 레이아웃 파일을 통해 구성되며, 관련 레이아웃 관리자도 프로그램을 통해 제어할 수 있습니다.

package com.example.relativelayoutproject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RelativeLayout;

public class RelativeLayoutActivity extends Activity {
	private RelativeLayout layout = null;
	private Button but2 = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_relative_layout);
		this.layout = (RelativeLayout)super.findViewById(R.id.mylayout);
		this.but2 = new Button(this);
		this.but2.setText("我也是一个按钮");
		RelativeLayout.LayoutParams butParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);//准备组件布局参数
		butParams.addRule(RelativeLayout.LEFT_OF, R.id.img);//添加规则放在text组件的右边
		butParams.addRule(RelativeLayout.BELOW, R.id.text);//添加规则放在text组件的下边
		this.layout.addView(this.but2,butParams);//加入组件
	}
}

 4.3 요약

(1) 상대 레이아웃 관리자는 컴포넌트 위치 지정을 위한 참조입니다.

(2) RelativeLayout 및 RelativeLayout.LayoutParams 클래스를 사용하여 활동 프로그램에서 레이아웃 관리자를 동적으로 구성합니다.

Supongo que te gusta

Origin blog.csdn.net/weixin_41830242/article/details/131242274
Recomendado
Clasificación