莆田做鞋有没有网站看,做类似美团的网站吗,如何规范网站使用,餐饮品牌设计公司哪家好layout#xff08;布局#xff09;定义了用户界面的可视化结构#xff08;visual structure#xff09;,如Activity的UI,应用窗口的UI。 有两种方式声明layout: 1.在xml文件中声明UI组件。 2.在运行时#xff0c;实例化布局元素。我们可以以编码的方式创建View或ViewGroup… layout布局定义了用户界面的可视化结构visual structure,如Activity的UI,应用窗口的UI。 有两种方式声明layout: 1.在xml文件中声明UI组件。 2.在运行时实例化布局元素。我们可以以编码的方式创建View或ViewGroup对象操纵它们的属性。 下面用一个小例子来学习怎样以编码的方式添加layout 1 import android.app.Activity;2 import android.graphics.Color;3 import android.os.Bundle;4 import android.view.ViewGroup;5 import android.widget.Button;6 import android.widget.LinearLayout;7 import android.widget.TextView;8 9 public class MainActivity extends Activity {
10
11 private LinearLayout linearLayout;
12 private TextView textView;
13 private Button button;
14 public static final int VERTICAL 1;
15 public static final int MATCH_PARENT -1;
16 public static final int WRAP_CONTENT -2;
17 Override
18 protected void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20
21 //以编码的方式添加layout
22
23 linearLayout new LinearLayout(this);
24 linearLayout.setOrientation(VERTICAL); //设置LinearLayout方向0是水平1是垂直。默认是水平。
25 //设置布局参数-1是MATCH_PARENT,-2是WRAP_CONTENT
26 //ViewGroup.LayoutParams(int width, int height)
27 linearLayout.setLayoutParams(new ViewGroup.LayoutParams(MATCH_PARENT,MATCH_PARENT));
28
29 textView new TextView(this);
30 textView.setText(ThisIsATextView);
31 textView.setBackgroundColor(Color.RED);
32 textView.setLayoutParams(new ViewGroup.LayoutParams(MATCH_PARENT,WRAP_CONTENT));
33
34 button new Button(this);
35 button.setText(ThisIsAButton);
36 button.setBackgroundColor(Color.GREEN);
37 button.setLayoutParams(new ViewGroup.LayoutParams(MATCH_PARENT,WRAP_CONTENT));
38
39 linearLayout.addView(button);
40 linearLayout.addView(textView);
41 //布局写好后不要忘记添加到Activity中
42 setContentView(linearLayout);
43
44
45 }
46 } 运行效果图 每个layout文件必须包含一个确定的根元素这个根元素它必须是View或ViewGroup的对象。 那View类和ViewGroup类的作用是什么呢 View: 为用户界面组件提供基本的搭建区域 。View类是widgets的父类widgets通常用来创建交互UI组件 如button,TextView等等。View类同时也是ViewGroup类的父类。 ViewGroup: 是layout类的父类而layout类是保存其他View或ViewGroup的可视化容器(invisible containers),并且能定义它们的布局属性。 通过添加额外的布局对象layout object或窗口(widgets)作为子元素来逐渐完善视图层。 下面通过一个layout文件来具体学习以下 1 !-- 确定的根元素 LinearLayout是ViewGroup的子类layout的对象 --2 LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android3 xmlns:toolshttp://schemas.android.com/tools4 android:layout_widthmatch_parent5 android:layout_heightwrap_content6 android:orientationvertical 7 !-- 添加子组件来丰富视图层 --8 Button9 android:layout_widthmatch_parent
10 android:layout_heightwrap_content
11 android:background#f00
12 android:layout_weight1
13 android:textThisIsAButton /
14 TextView
15 android:layout_widthmatch_parent
16 android:layout_heightwrap_content
17 android:background#0f0
18 android:textThisIsATextView
19 /
20 /LinearLayout 我们在xml文件中声明好界面的布局方式以后将xml文件保存在res/layout/ 下即可。 希望这篇文章对大家的学习有所帮助如果你喜欢请推荐一下谢谢~ 如果转载请在文章开头处注明本博客地址:http:www.cnblogs.com/JohnTsai 欢迎讨论交流邮箱:JohnTsai.Workgmail.com