实验 2 Android布局管理器的应用

1.shiyan0201

android:text="@string/name" 要重定义自己的文本

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/name"
        android:textSize="30sp"
        android:textColor="#ff0"/>
    <TextView
        android:id="@+id/sno"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sno"
        android:textSize="30sp"
        android:textColor="#ff0"/>
    <TextView
        android:id="@+id/class_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/class_name"
        android:textSize="30sp"
        android:textColor="#ff0"/>

</LinearLayout>

2.shiyan0202 例题1-5

android:text="@string/name" 要重定义自己的文本
【例1】

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/buton1"
        android:layout_weight="2"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:background="#1dd1a1"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/but4"
            android:layout_gravity="center_vertical"
            />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/but5"
            android:layout_gravity="bottom"
            />
    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/buton2"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/buton3"
        android:layout_weight="2"
        />
</LinearLayout>

在这里插入图片描述
【例2】

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn1"
        android:layout_margin="1dp"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn2"
        android:layout_centerHorizontal="true"
        android:layout_margin="1dp"/>
    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn3"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_margin="1dp"/>
    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn4"
        android:layout_centerVertical="true"
        android:layout_margin="1dp"/>
    <Button
        android:id="@+id/btn5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn5"
        android:layout_centerInParent="true"
        android:layout_margin="1dp"/>
    <Button
        android:id="@+id/btn6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn6"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_margin="1dp"/>
    <Button
        android:id="@+id/btn7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn7"
        android:layout_alignParentBottom="true"
        android:layout_margin="1dp"/>
    <Button
        android:id="@+id/btn8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn8"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="1dp"/>
    <Button
        android:id="@+id/btn9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn9"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_margin="1dp"/>
    <Button
        android:id="@+id/btn10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn10"
        android:layout_toRightOf="@id/btn1"
        android:layout_alignRight="@id/btn2"
        android:layout_below="@id/btn2"
        android:layout_above="@id/btn5"
        android:layout_toEndOf="@id/btn1"
        android:layout_alignEnd="@id/btn2"
        android:layout_margin="1dp"/>
    <Button
        android:id="@+id/btn11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn11"
        android:layout_alignTop="@id/btn4"
        android:layout_alignLeft="@id/btn10"
        android:layout_toLeftOf="@id/btn5"
        android:layout_above="@id/btn7"
        android:layout_alignStart="@id/btn10"
        android:layout_toStartOf="@id/btn5"
        android:layout_margin="1dp"/>
    <Button
        android:id="@+id/btn12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn12"
        android:layout_alignLeft="@id/btn5"
        android:layout_below="@id/btn5"
        android:layout_toLeftOf="@id/btn6"
        android:layout_alignBaseline="@id/btn11"
        android:layout_margin="1dp"/>

</RelativeLayout>

在这里插入图片描述
【例3】

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    android:stretchColumns="1,2">

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/a"/>
        <EditText
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:hint="@string/x"/>
        <EditText
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:hint="@string/y"/>
    </TableRow>

    <TableRow>
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/b"/>
        <EditText
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:hint="@string/x"/>
        <EditText
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:hint="@string/y"/>
    </TableRow>

    <Button
        android:text="@string/queding"/>

</TableLayout>

在这里插入图片描述
【例4】
android:foreground="@drawable/ic_baseline_adb_24" 在drawable目录下选张图即可

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foreground="@drawable/ic_baseline_adb_24"
    android:foregroundGravity="bottom|right">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ic_launcher_foreground"/>
    <TextView
        android:layout_width="280dp"
        android:layout_height="280dp"
        android:layout_gravity="center"
        android:background="#f00"
        android:text="红色背景的TextView"
        />
    <TextView
        android:layout_width="230dp"
        android:layout_height="230dp"
        android:layout_gravity="center"
        android:background="#ff9f43"
        android:text="橙色背景的TextView"/>
    <TextView
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:background="#ff0"
        android:layout_gravity="center"
        android:text="黄色背景的TextView"/>

</FrameLayout>

在这里插入图片描述

【例5】

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:columnCount="4"
    android:rowCount="6"
    android:useDefaultMargins="true"
    android:layout_gravity="center_horizontal">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="3"
        android:text="@string/bt1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:layout_column="0"
        android:text="@string/bt2"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:layout_column="1"
        android:text="@string/bt3"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:layout_column="2"
        android:text="@string/bt4"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:layout_column="3"
        android:text="@string/bt5"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="2"
        android:layout_column="0"
        android:text="@string/bt6"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="2"
        android:layout_column="1"
        android:text="@string/bt7"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="2"
        android:layout_column="2"
        android:text="@string/bt8"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="2"
        android:layout_column="3"
        android:text="@string/bt9"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="3"
        android:layout_column="0"
        android:text="@string/bt10"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="3"
        android:layout_column="1"
        android:text="@string/bt11"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="3"
        android:layout_column="2"
        android:text="@string/bt12"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="4"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:text="@string/bt13"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="4"
        android:layout_column="2"
        android:text="@string/bt14"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="4"
        android:layout_column="3"
        android:layout_rowSpan="2"
        android:layout_gravity="fill"
        android:text="@string/bt15"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="5"
        android:layout_column="0"
        android:layout_columnSpan="3"
        android:layout_gravity="fill"
        android:text="@string/bt16"/>

</GridLayout>

在这里插入图片描述

3.练习教材中布局管理部分的实例

android:text="@string/name" 要重定义自己的文本
【例题5-1】

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".example_5_1"
    android:padding="5dp">

    <TextView
        android:id="@+id/tvPwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tv_password"
        android:textSize="25sp"
        android:textColor="#8C6931"/>

    <EditText
        android:id="@+id/txtPwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tvPwd"
        android:inputType="textPassword"
        android:hint="@string/hint_password"/>

    <Button
        android:id="@+id/btnsetCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtPwd"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dp"
        android:text="@string/cancel"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="10dp" />

    <Button
        android:id="@+id/btnSet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtPwd"
        android:layout_toLeftOf="@id/btnsetCancel"
        android:text="@string/confirm"
        android:layout_toStartOf="@id/btnsetCancel" />


</RelativeLayout>

【例题5-2】

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context=".example_5_2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="新增便签"
        android:textSize="40sp"
        android:textStyle="bold"/>

    <TextView
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:text="请输入便签,最多输入200字"
        android:textColor="#8C6931"
        android:textSize="22sp"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:lines="10"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp">

        <Button
            android:id="@+id/btnFlagCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="10dp"
            android:layout_alignParentEnd="true"
            android:layout_marginStart="10dp"
            android:text="取消"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/btnFlagCancel"
            android:text="@string/save"
            android:layout_toStartOf="@id/btnFlagCancel" />
    </RelativeLayout>


</LinearLayout>

【例题5-3】

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foreground="@drawable/ic_android_black_24dp"
    android:foregroundGravity="bottom|right"
    tools:context=".example_5_3">

    <TextView
        android:layout_width="280dp"
        android:layout_height="280dp"
        android:layout_gravity="center"
        android:background="#ff0000ff"
        android:textColor="#fff"
        android:text="蓝色背景的TextView"/>

    <TextView
        android:layout_width="230dp"
        android:layout_height="230dp"
        android:layout_gravity="center"
        android:background="#ff0077ff"
        android:textColor="#fff"
        android:text="天蓝色背景的TextView"/>

    <TextView
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:layout_gravity="center"
        android:background="#ff00b4ff"
        android:textColor="#fff"
        android:text="水蓝色背景的TextView"/>

</FrameLayout>

【例题5-4】

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:stretchColumns="0,3"
    tools:context=".example_5_4">

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/admin"
            android:textSize="18sp"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="200dp"
            android:textSize="18sp"/>

        <TextView/>
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/password_5_4"
            android:textSize="18sp"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:textSize="18sp"/>

        <TextView/>
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/login"
            android:layout_margin="1dp"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/quit"
            android:layout_margin="1dp"/>

    </TableRow>

</TableLayout>

总结

代码是android.studio粘贴过来的,和eclipse可能不太一样。做错了就错了吧,小错误无伤大雅~~~
代码中的android:text="@string/name" 要重定义自己的文本。

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
Android 开发 (实验五) 实验题目:Android 组件布局试验 指导老师: 班 级:计算机科学与技术系班 姓 名: 一、实验目的 1、掌握 Android 组件布局的使用方法 2、学会组件布局的重要属性与应用 3、能够根据需求,通过布局构建各类实际的页面。 二、实验内容 组 件 布 局 有 : LinearLayout 、 TableLayout 、 FrameLayout 、 RelativeLayout 三、实验步骤 1、用表格布局完成登录界面 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFFCC" android:stretchColumns="0,3" > <ImageView android:layout_width="fill_parent" android:layout_height="5dp"/> <TableRow> <ImageView android:layout_width="wrap_content" android:layout_height="100dp" android:src="@drawable/img" android:gravity="center" android:layout_span="4"/> </TableRow> <TableRow > <TextView/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:text="@string/username" android:textStyle="bold" android:textColor="#000000"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:minWidth="200px" android:id="@+id/ETname"/> </TableRow> <TableRow > <TextView/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:text="@string/password" android:textStyle="bold" android:textColor="#000000"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:minWidth="200px" android:inputType="textPassword" android:id="@+id/ETpwd"/> </TableRow> <TableRow> <TextView /> <Button android:id="@+id/Btok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right" android:text="@string/button" android:textStyle="bold"/> <Button android:id="@+id/Btcancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:text="@string/cancel" android:textStyle="bold"/> </TableRow> </TableLayout> 2、使用线性布局完成系统主界面 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值