Activity窗口对象可以通过调用 setContentView()设置要显示的控件,但只能设置一个View对象。如窗口上有多个按钮等多个控件时,则需要使用布局对象来管理控件的显示位置及对齐方式.
布局控件等的类有: ConstraintLayout, GridLayout, FrameLayout, LinearLayout, RelativeLayout, TableLayout等.
可创建一个布局类对象用于管理多个控件。如实现用LinearLayout对象来管理两个按钮控件, 按水平排列:
工程沿用上一个例子:
/* MyActivity.java */
mport android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyActivity extends Activity {
private Button btn, btn2;
private LinearLayout layout;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //调用父类的功能
layout = new LinearLayout(this); //创建布局类对象
layout.setOrientation(LinearLayout.HORIZONTAL); //设置水平布局
btn = new Button(this); //创建按钮对象
btn.setText("btn1"); //设置按钮显示的文本
btn2 = new Button(this);
btn2.setText("btn2");
layout.addView(btn); //让布局管理器管理按钮
layout.addView(btn2);
setContentView(layout);
}
}
效果图:
布局管理器还可以通过setLayoutParams()来设置布局管理器对窗口的布局范围。
其中布局的宽度和高度的选择可以是MATCH_PARENT和WRAP_CONTENT.
MATCH_PARENT是表示占完用上一级窗口或布局管理器的空间.
WRAP_CONTENT是表示根据要显示的内容调整实际的占用空间.
如一个LinearLayout对象管理整个窗口的空间,则可以:
layout = new LinearLayout(this); //创建布局类对象
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);
setContentView(layout);
再如用LinearLayout对象水平布局两个按钮对象, 则每个按钮对象都需要setLayoutParams()来设置布局参数. 实现一个按钮布局管理器的30%宽度, 一个按钮占70%宽度, 两个按钮的高度与布局管理器的高度一致
/* MyActivity.java */
package com.example.jk.a01hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyActivity extends Activity {
private Button btn, btn2;
private LinearLayout layout;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //调用父类的功能
layout = new LinearLayout(this); //创建布局类对象
layout.setOrientation(LinearLayout.HORIZONTAL); //设置水平布局
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);
btn = new Button(this);
btn.setText("btn1");
btn2 = new Button(this);
btn2.setText("btn2");
btn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 30));
btn2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 70));
layout.addView(btn); //让布局管理器管理按钮
layout.addView(btn2);
setContentView(layout);
}
}
效果图:
而且布局管理器还可以嵌套使用。如在窗口上实现4个按钮分成每两个按钮一行, 其中第一行占窗口的30%高度,剩下的高度由第二行占用. 而且第一行的两个按钮,一个占40%宽度,另一个占用60%宽度。第二行的两个按钮占用的宽度都是50%.
每一行的两个按钮对象由一个水平的LinearLayout对象来管理, 共需要两个水平的LinearLayout布局管理器,最后由一个垂直的LinearLayout对象来管理这两个水平的布局管理器.
/* MyActivity.java */
package com.example.jk.a01hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyActivity extends Activity {
private Button btn1, btn2, btn3, btn4;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //调用父类的功能
LinearLayout hlayout1, hlayout2, vlayout;
hlayout1 = new LinearLayout(this);
hlayout1.setOrientation(LinearLayout.HORIZONTAL);
hlayout2 = new LinearLayout(this);
hlayout2.setOrientation(LinearLayout.HORIZONTAL);
vlayout = new LinearLayout(this);
vlayout.setOrientation(LinearLayout.VERTICAL);
btn1 = new Button(this);
btn1.setText("btn1");
btn2 = new Button(this);
btn2.setText("btn2");
btn3 = new Button(this);
btn3.setText("btn3");
btn4 = new Button(this);
btn4.setText("btn4");
hlayout1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT, 30));
hlayout2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT, 70));
vlayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
btn1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 40));
btn2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 60));
hlayout1.addView(btn1);
hlayout1.addView(btn2);
btn3.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 50));
btn4.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 50));
hlayout2.addView(btn3);
hlayout2.addView(btn4);
vlayout.addView(hlayout1);
vlayout.addView(hlayout2);
setContentView(vlayout);
}
}
效果图: