Android系统开发02—Android布局管理器

Android系统开发02—Android布局管理器

1. View,ViewGroup
1) View类
View类为所有可视化控件的基类,主要提供了空间绘制和事件处理。
2)ViewGroup类
ViewGroup类也是View类的子类,但是可以充当其他控件的容器。

2. 线性布局
线性布局是最简单的布局,它提供了控件水平或者垂直排列的模型。使用此布局时可以通过设置控件的weight参数控制各个控件在容器中的相对大小。
线性布局实例:
string.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">LinearExample</string>
<string name="button">按钮</string>
<string name="add">添加</string>
</resources>

main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/lla" android:orientation="vertical" android:gravity="right">
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/Button01" android:text="@string/add"></Button>
</LinearLayout>

MyAndroidProject.java:
package qijia.si;

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


public class MyAndroidProject extends Activity {
int count = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.Button01);

btn.setOnClickListener(
new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout ll = (LinearLayout) findViewById(R.id.lla);

String msg = MyAndroidProject.this.getResources().getString(R.string.button);
Button tempbutton = new Button(MyAndroidProject.this);

tempbutton.setText(msg+(++count));
tempbutton.setWidth(80);
ll.addView(tempbutton);

}
}




);


}
}
3. 表格布局
TableLayout类以行和列的形式管理控件,每行为一个TableRow对象,也可以为一个View对象,当为View对象时,该View对象将跨越该行的所有列。在TableRow中可以添加子空间,每添加一个子空间为一列。
在TableLayout中,可以为列设置三种属性:
1) Shrinkable,该列宽度可以进行收缩,以使表格能够适应期父容器的大小
2) Stretchable,该列的宽度可以进行拉伸,以使填满表格中空闲的空间。
3) Collapsed,该列将被隐藏。
注意:一个列可以同时拥有1,2的属性。
案例:
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
<color name="red">#ff0000</color>
<color name="black">#000000</color>
<color name="green">#00ff00</color>
<color name="blue">#0000ff</color>
</resources>

string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TableExample</string>
<string name="tv1">我自己是一行…………我自己是一行</string>
<string name="tvShort">我的内容少</string>
<string name="tvStrech">我是被拉伸的一行</string>
<string name="tvShrink">我是被收缩的一行被收缩的一行</string>
<string name="tvLong">我的内容很多很多很多很多很多很多</string>
</resources>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/pic" android:gravity="bottom">
<TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/TableLayout03" android:background="@color/white" android:stretchColumns="0" android:collapseColumns="1">
</TableLayout>
<TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/TableLayout02" android:background="@color/white" android:stretchColumns="0">
<TableRow android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TableRow01">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/TextView02" android:text="@string/tvStrech" android:layout_margin="4px" android:background="@color/green" android:textColor="@color/blue"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/TextView03" android:text="@string/tvShort" android:textColor="@color/black" android:background="@color/blue" android:layout_margin="4px"></TextView>
</TableRow>
</TableLayout>
<TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/TableLayout01" android:background="@color/white">
<TextView android:textColor="@color/black" android:layout_margin="4px" android:text="@string/tv1" android:background="@color/red" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TextView01"></TextView>
</TableLayout>
</LinearLayout>

MyAndroidProject.java
package qijia.si;

import android.app.Activity;
import android.os.Bundle;


public class MyAndroidProject extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}
}

4. 相对布局
在相对布局中,子控件的位置是相对兄弟控件或父容器而决定的。
需要注意的是,相对布局中要避免出现循环依赖。
5. 帧布局
FrameLayout帧布局在屏幕上开辟出了一块区域,在这块区域中可以添加多个子控件,但是所有的子空间都被对齐到屏幕的左上角。帧布局的大小由子控件中尺寸最大的那个控件决定。
6. 绝对布局
绝对布局是指屏幕中所有控件的摆放由开发人员通过设置控件的坐标来指定,控件容器不再负责管理其子控件的位置。
案例:
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AbsoluteExample</string>
<string name="uid">用户名</string>
<string name="pwd">密码</string>
<string name="ok">确定</string>
<string name="cancel">取消</string>
</resources>

color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
<color name="red">#ff0000</color>
<color name="black">#000000</color>
<color name="green">#00ff00</color>
<color name="blue">#0000ff</color>
</resources>

main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:background="@color/white" android:id="@+id/absoluteLayout1" android:layout_height="fill_parent">
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_x="20dip" android:layout_y="20dip" android:id="@+id/TextView01" android:text="@string/uid"></TextView>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TextView02" android:text="@string/pwd" android:layout_x="20dip" android:layout_y="80dip"></TextView>
<EditText android:text="EditText" android:layout_height="wrap_content" android:layout_y="20dip" android:id="@+id/EditText01" android:layout_x="80dip" android:layout_width="180dip"></EditText>
<EditText android:text="EditText" android:layout_height="wrap_content" android:id="@+id/EditText02" android:password="true" android:layout_x="80dip" android:layout_y="80dip" android:layout_width="180dip"></EditText>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/Button01" android:text="@string/ok" android:layout_x="155dip" android:layout_y="140dip"></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/Button02" android:text="@string/cancel" android:layout_x="210dip" android:layout_y="140dip"></Button>
<ScrollView android:layout_x="10dip" android:layout_y="200dip" android:layout_height="150dip" android:layout_width="250dip" android:id="@+id/ScrollView01">
<EditText android:text="EditText" android:layout_height="wrap_content" android:layout_width="fill_parent" android:singleLine="false" android:gravity="top" android:id="@+id/EditText03"></EditText>
</ScrollView>
</AbsoluteLayout>

MyAndroidProject.java
package qijia.si;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MyAndroidProject extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button okButton = (Button) findViewById(R.id.Button01);
final Button cancelButton = (Button) findViewById(R.id.Button02);

final EditText uid = (EditText) findViewById(R.id.EditText01);
final EditText pwd = (EditText) findViewById(R.id.EditText02);
final EditText log = (EditText) findViewById(R.id.EditText03);

okButton.setOnClickListener(
new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
String uidStr = uid.getText().toString();
String pwdStr = pwd.getText().toString();

log.append("用户名:"+uidStr+"密码:"+pwdStr+"\n");

}
}

);

cancelButton.setOnClickListener(
new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
uid.setText("");
pwd.setText("");
}
}


);



}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值