Android番外篇——XML layout与CSS 转载

本文详细解析了Android中RelativeLayout的使用方法及特点,通过实例说明如何利用各种布局属性来精确控制控件的位置,同时对比了RelativeLayout与CSS相对定位的相似之处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   开发过Android应用的同学们都知道,Android工程-res-layout资源文件夹下存放着控制view布局的xml文件,我们可以同过getViewById(int i)方法,从XML中构造view及其子类,在这个过程当中,XML文件中的一切layout属性也将被赋予这个view。当然,我们也能够通过代码来为某一个view来设置layout,那是后话。通过对集中layout的分析和比较我发现,Android中AbsoluteLayout与CSS的绝对定位很像,TableLayout与HTML的表格定位很像,而RelativeLayout与CSS的相对定位很像。前两者都已经是老生常谈了,我重点比较一下最后一对,即RelativeLayout与CSS的相对定位(Position:relative)。先看一段XML:

<?xml version="1.0" encoding="utf-8"?>
< RelativeLayout
    
xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width
="fill_parent" android:layout_height="fill_parent"
    android:orientation
="vertical">
    
<RadioGroup android:id="@+id/radioGroup"
        android:layout_width
="fill_parent" android:layout_height="167px"
        xmlns:android
="http://schemas.android.com/apk/res/android"
        android:orientation
="vertical">
        
<RadioButton android:id="@+id/Queen"
            android:layout_width
="wrap_content"
            android:layout_height
="wrap_content" android:text="@string/Queen">
        
</RadioButton>
        
<RadioButton android:id="@+id/Rook"
            android:layout_width
="wrap_content"
            android:layout_height
="wrap_content" android:text="@string/Rook">
        
</RadioButton>
        
<RadioButton android:id="@+id/Knight"
            android:layout_width
="wrap_content"
            android:layout_height
="wrap_content" android:text="@string/Knight">
        
</RadioButton>
        
<RadioButton android:id="@+id/Bishop"
            android:layout_width
="wrap_content"
            android:layout_height
="wrap_content" android:text="@string/Bishop">
        
</RadioButton>
    
</RadioGroup>
    
<Button android:id="@+id/promotion"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content" android:layout_below="@id/radioGroup"
        android:text
="@string/promote" />
</RelativeLayout>


上面一段XML,这是我为Android国际象棋游戏中的一个“兵行底线”编写的Custom Dialog(自定义对话框)的XML layout。在这里简单定义了几个控件(来自于android.wedget包)的一些位置和外观。可以看到,根节点RelativeLayout告诉我们这是相对定位的布局,然后从上至下由一个RadioGroup(内含四个RadioButton)以及一个Button。通过最下面的android:layout_below属性,告诉Button要在radioGroup下面呈现。

这是一个非常简单的属性,在relativeLayout下,还有很多属性,见下表

android:layout_above                        Positions the bottom edge of this view above the given anchor view ID. 
android:layout_alignBaseline             Positions the baseline of this view on the baseline of the given anchor view ID. 
android:layout_alignBottom               Makes the bottom edge of this view match the bottom edge of the given anchor view ID. 
android:layout_alignLeft                    Makes the left edge of this view match the left edge of the given anchor view ID. 
android:layout_alignParentBottom    If true, makes the bottom edge of this view match the bottom edge of the parent. 
android:layout_alignParentLeft         If true, makes the left edge of this view match the left edge of the parent. 
android:layout_alignParentRight       If true, makes the right edge of this view match the right edge of the parent. 
android:layout_alignParentTop         If true, makes the top edge of this view match the top edge of the parent. 
android:layout_alignRight                 Makes the right edge of this view match the right edge of the given anchor view ID. 
android:layout_alignTop                   Makes the top edge of this view match the top edge of the given anchor view ID. 
android:layout_below                      Positions the top edge of this view below the given anchor view ID. 
android:layout_centerHorizontal     If true, centers this child horizontally within its parent. 
android:layout_centerInParent       If true, centers this child horizontally and vertically within its parent. 
android:layout_centerVertical         If true, centers this child vertically within its parent. 
android:layout_toLeft                     Positions the right edge of this view to the left of the given anchor view ID. 
android:layout_toRight                   Positions the left edge of this view to the right of the given anchor view ID. 

我相信大家的英文水平,这段从API上抄下来的东西我就不翻译了。但是可以看出来,只要设置上面的属性(不一定要全部),我们可以轻松规定一个控件(View)的在相对定位下的显示行为。同时RelativeLayout是从layout上继承下来的,因此layout的很多属性也可以在这里使用,包括:
android:layout_height, android:layout_width,

### 如何使用Docker启动Linux容器 #### 创建并运行基础的Ubuntu容器 为了展示如何使用 Docker 启动 Linux 容器,这里以 Ubuntu 镜像为例来创建启动一个简单的容器。 ```bash docker run -it ubuntu:latest /bin/bash ``` 这条命令会拉取最新的官方 Ubuntu 镜像,并启动一个新的交互式的 Bash shell 实例[^1]。 #### 使用特定版本的基础镜像 如果需要指定某个具体版本的 Linux 发行版作为基础镜像,则可以在 `run` 命令中指明标签名: ```bash docker pull centos:7 docker run -d --name mycentos centos:7 sleep infinity ``` 上述例子展示了怎样下载 CentOS 7 版本的镜像以及以后台模式启动它。注意这里的 `-d` 参数表示守护进程方式运行;而 `sleep infinity` 是为了让容器保持活动状态而不立即退出。 #### 构建自定义应用环境 对于更复杂的应用场景来说,通常还需要安装额外软件服务到基本操作系统之上。这时可以编写 Dockerfile 文件来自动生成带有预配置应用程序栈的新镜像。 例如,在 Tomcat 场景下,先在主机上准备好的项目根目录里新建名为 `Dockerfile` 的文本文件,编辑其内容如下所示[^2]: ```Dockerfile FROM tomcat:9.0 COPY ./webapps/ROOT.war /usr/local/tomcat/webapps/ EXPOSE 8080 CMD ["catalina.sh", "run"] ``` 接着在同一路径执行构建操作: ```bash docker build -t custom-tomcat . ``` 最后就可以通过下面的方式启动这个定制化的 Web 应用了: ```bash docker run -p 8080:8080 --name webapp-container custom-tomcat ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值