500字范文,内容丰富有趣,生活中的好帮手!
500字范文 > android开发4:Android布局管理器1(线性布局 相对布局RelativeLayout-案例)

android开发4:Android布局管理器1(线性布局 相对布局RelativeLayout-案例)

时间:2023-04-04 06:05:49

相关推荐

android开发4:Android布局管理器1(线性布局 相对布局RelativeLayout-案例)

控件类概述

View

可视化控件的基类

ViewGroup

View的子类,但是它可以充当其他控件的容器

ViewGroup的子控件既可以是普通的View,也可以是ViewGroup。一些高级控件如Gallery、GridView等都是继承自ViewGroup。Android中为每种不同的布局提供一个ViewGroup的子类,如LinearLayout、TableLayout、RelativeLayout、FrameLayout、AbsoluteLayout等。

线性布局android.widget.LinearLayout

参考API:

/reference/android/widget/LinearLayout.html

/reference/android/widget/LinearLayout.LayoutParams.html

LinearLayout是最简单的布局之一,它提供了控件水平或垂直排列的模型,可以通过设置控件的weight参数控制各个控件在容器中的相对大小。LinearLayout布局的属性也是既可以通过布局XML文件设置,也可以通过成员方法进行设置。

LinearLayout常用的属性及对应设置方法

gravity可取的属性及说明

相对布局android.widget.RelativeLayout

对应API参考:

/reference/android/widget/RelativeLayout.html

/reference/android/widget/RelativeLayout.LayoutParams.html 奉献一个实例:

XML布局配置

<RelativeLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:gravity="left"android:id="@+id/lls"tools:context=".MainActivity" ><Buttonandroid:id="@+id/button01"android:text="@string/add"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="10dp"/></RelativeLayout>

Activity

package com.example.android_layout;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.RelativeLayout;/*** Button基本使用方法* 1、添加Button控件到XMl布局文件中* 2、给按钮一个id号,这是按钮唯一的名字* 3、通过程序获取按钮* 4、处理按钮点击* ①第一种是通过onClick属性,通过这个属性设置处理点击事件的方法名,在Activity中实现这个方法。* ②另一种方法是典型的事件监听机制的应用形式,使用setOnClickListener添加监听器对象* @author liuxinming**/public class MainActivity extends Activity {int count = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取屏幕控件Button button = (Button) findViewById(R.id.button01);//添加button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//获取布局管理器RelativeLayout ll = (RelativeLayout) findViewById(R.id.lls);//动态创建button 对象String msg = MainActivity.this.getString(R.string.app_name);Button tempbutton = new Button(MainActivity.this);tempbutton.setText(msg+(++count));tempbutton.setId(count);/*** LayoutParams继承于Android.View.ViewGroup.LayoutParams.* LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout占领的,如果将一个View添加到一个Layout中,最好告诉Layout用户期望的布局方式,也就是将一个认可的layoutParams传递进去。可以这样去形容LayoutParams,在象棋的棋盘上,每个棋子都占据一个位置,也就是每个棋子都有一个位置的信息,如这个棋子在4行4列,这里的“4行4列”就是棋子的LayoutParams。**********RelativeLayout下动态设置子控件居中**********/RelativeLayout.LayoutParams buttonLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);buttonLayoutParams.addRule(RelativeLayout.BELOW,tempbutton.getId());buttonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); buttonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); tempbutton.setLayoutParams(buttonLayoutParams);ll.addView(tempbutton,buttonLayoutParams);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

上面案例是布局一个按钮,然后通过Activity设置一个监听按钮事件,点击后添加一个新按钮出来 布局API属性比较多,简单弄了下,没有深入写,目前是添加的按钮全部覆盖起来了。哈哈 有需要的朋友,可以自己研究下属性,设置添加后的位置。 测试版本:Android4.3 API18

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。