Android学习第二天------布局方式(相对布局、网格布局、绝对布局)

news/2024/7/6 1:48:15

一、相对布局(RelativeLayout)

相对布局指的是,组件的位置相对于父容器或兄弟组件的位置而定。

XML文件控制

下面的代码表示创建五个按钮分别放置四角和中心

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >
    <!--  android:gravity="right"
    android:ignoreGravity="@+id/b1" -->
    <Button 
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        />
     <Button 
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="2"
        />
      <Button
        android:id="@+id/b3" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/b1"
        android:layout_centerInParent="true"
        android:text="3"
        />
       <Button
        android:id="@+id/b4" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/b3"
        android:layout_alignParentRight="true"
        android:text="4"
        />
        <Button 
        android:id="@+id/b5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/b3"
        android:text="5"
        />

</RelativeLayout>
java代码实现

//创建布局容器
		RelativeLayout relativeLayout=new RelativeLayout(this);
		//创建按钮
		Button button=new Button(this);
		button.setText("按钮1");
		//设置按钮id
		button.setId(1);
		//设置组件宽度,高度
		RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		//设置组件的相对位置
		layoutParams.addRule(RelativeLayout.BELOW, button.getId());
		Button button2=new Button(this);
		button2.setText("按钮2");
		button2.setLayoutParams(layoutParams);
		relativeLayout.addView(button);
		relativeLayout.addView(button2);
		setContentView(relativeLayout);
二、网格布局(GridLayout)

网格布局是将页面分割成几行几列的空间

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="3"列数
    android:rowCount="3"行数
     >
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"在第0列(行数列数都是从0开始)
        android:layout_row="0"在第0行
        android:text="1"
        />
	 <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="1"
        android:text="2"
        />
	  <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="1"
        android:text="3"
        />
	   <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="0"
        android:text="4"
        />

	   <Button
	       android:layout_width="wrap_content"
	       android:layout_height="wrap_content"
	       android:layout_column="1"
	       android:layout_row="0"
	       android:layout_rowSpan="2"合并两行
	       android:layout_gravity="fill"填充于父容器
	       android:text="5" />
	   
</GridLayout>
java代码实现

//创建网格布局
		GridLayout gridLayout=new GridLayout(this);
		//设置行数列数
		gridLayout.setRowCount(2);
		gridLayout.setColumnCount(3);
		//设置组件放的位置
		GridLayout.Spec rowcount=GridLayout.spec(0);
		GridLayout.Spec colcount=GridLayout.spec(0);
		GridLayout.LayoutParams layoutParams=new GridLayout.LayoutParams(rowcount,colcount);
		
		GridLayout.Spec rowcount1=GridLayout.spec(1);
		GridLayout.Spec colcount1=GridLayout.spec(2);
		GridLayout.LayoutParams layoutParams1=new GridLayout.LayoutParams(rowcount1,colcount1);
		Button button=new Button(this);
		button.setText("1");
		button.setLayoutParams(layoutParams);
		gridLayout.addView(button);
		
		Button button1=new Button(this);
		button1.setText("2");
		button1.setLayoutParams(layoutParams1);
		gridLayout.addView(button1);
		
		setContentView(gridLayout);
三、绝对布局

通过坐标设定组件的位置

XML文件控制

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="100dp"
        android:layout_y="200dp"
        />

</AbsoluteLayout>

java代码实现

AbsoluteLayout absoluteLayout=new AbsoluteLayout(this);
		Button button=new Button(this);
		button.setX(100.0f);
		button.setY(200.0f);
		absoluteLayout.addView(button);
		setContentView(absoluteLayout);







http://www.niftyadmin.cn/n/3654903.html

相关文章

boost源码剖析之:泛型编程精灵type_traits(rev#2)

boost源码剖析之&#xff1a;泛型编程精灵type_traits(rev#2)刘未鹏C的罗浮宫(http://blog.csdn.net/pongba)动机使用traits的动机一般有三种&#xff0c;分派、效率、使某些代码通过编译。分派下面有一个模板函数&#xff0c;假设一个动物收容组织提供了它&#xff0c;他们接受…

Android学习第三天————ListView组件

ListView是将内容以列表的形式显示出来&#xff0c;它能够自适应内容的长度 一、下面是通过XML文件创建一个ListView <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"android:layout_hei…

Android中实现横屏竖屏的切换

<span style"font-size:18px;">Android中横屏竖屏的切换</span> <span style"font-size:18px;"></span> <span style"font-size:18px;"></span> <span style"font-size:18px;">Configurati…

安全的线程同步

安全的线程同步Jeffrey Richter 著刘未鹏 译[msdn.2003.01]到目前为止&#xff0c;线程同步最为普遍的用途是确保多线程对共享资源的互斥访问。对于同步单个进程中的多个线程以实现互斥访问&#xff0c;Win32 API中的CRITICAL_SECTION结构以及与它相关的函数提供了最为快速和高…

Android学习第四天————ListView用BaseAdapter适配器来填充数据

一、BastAdapter适配器获得数据&#xff0c;用来填充ListView组件 1、通过XML文件来创建ListView组件 示例代码 <RelativeLayout xmlns:android"http://schemas.android.com/apk/res/android"xmlns:tools"http://schemas.android.com/tools"android:l…

Who is const ?!铪铪铪铪铪铪

这篇文章主要是看了王咏刚先生的一篇“关于C模板和重载的小问题”而发的&#xff0c;可以说是一篇回贴&#xff0c;之所以独立出来是因为这个问题“有趣”:)先引用一下王咏刚先生的例子&#xff1a;class Node{public: int m; Node(int value) : m(value) {} friend ostream&am…

开始用Google Code的Issue

曾经写了“开源&#xff0c;选择Google Code还是Sourceforge&#xff1f;”&#xff0c;Google Code vs. Sourceforge&#xff0c;这不过是一年&#xff08;未到&#xff09;前的事情&#xff0c;如今Google Code又今非昔比了。打算抽时间好好写一篇更新版的&#xff0c;以免误…

Android学习第四天————GridView组件

GridView类似于ListView&#xff0c;不过它可以一列显示多个组件 一、GridView的常用属性 <GridView android:id"id/gridview"android:layout_width"wrap_content"android:layout_height"wrap_content"android:verticalSpacing"10dp&qu…