安卓开发 各种布局xml代码示例 【原创】

垂直线性布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    >
    <Button android:text="按钮1" android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button android:text="按钮2" android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button android:text="按钮3" android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button android:text="按钮4" android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button android:text="按钮5" android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
        
</LinearLayout>


水平线性布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    >
    <Button android:text="按钮1" android:layout_width="wrap_content"
        android:layout_height="match_parent"/>
    <Button android:text="按钮2" android:layout_width="wrap_content"
        android:layout_height="match_parent"/>
    <Button android:text="按钮3" android:layout_width="wrap_content"
        android:layout_height="match_parent"/>
    <Button android:text="按钮4" android:layout_width="wrap_content"
        android:layout_height="match_parent"/>
    <Button android:text="按钮5" android:layout_width="wrap_content"
        android:layout_height="match_parent"/>
        
</LinearLayout>


表格布局的登录页面

注意:android:stretchColumns="0,3"允许第一列和第四列伸缩,以配合为左右居中显示

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:gravity="center_vertical"
    android:stretchColumns="0,3"
    >
    <!-- 第一行 -->
    <TableRow android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView />
        <TextView android:text="用户名:"
            android:layout_width="wrap_content"
            android:textSize="24sp"
            android:layout_height="wrap_content"/>
        <EditText android:layout_width="wrap_content"
            android:textSize="24sp"
            android:layout_height="wrap_content"
            android:minWidth="200sp"/>
       
        <TextView />
    </TableRow>
    <TableRow android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView />
        <TextView android:text="密    码:"
            android:layout_width="wrap_content"
            android:textSize="24sp"
            android:layout_height="wrap_content"/>
        <EditText android:layout_width="wrap_content"
            android:textSize="24sp"
            android:layout_height="wrap_content"
            android:minWidth="200sp"
            android:inputType="textPassword"/>
       
        <TextView />
    </TableRow>
    <TableRow android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView />
        <Button android:text="注 册"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button android:text="登 录"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
       
        <TextView />
    </TableRow>
</TableLayout>


帧布局:

<?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:background="@drawable/background"
    android:id="@+id/mylayout"
    android:foreground="@drawable/ic_launcher"
    android:foregroundGravity="bottom|right"
    >
    
    <TextView android:text="红色背景的textView,显示在最下层" 
        android:background="#FFFF0000"
        android:layout_gravity="center"
        android:layout_width="380sp"
        android:layout_height="380sp"/>
    <TextView android:text="橙色背景的textView,显示在2层" 
        android:background="#FFFF6600"
        android:layout_gravity="center"
        android:layout_width="300sp"
        android:layout_height="300sp"/>
    <TextView android:text="黄色背景的textView,显示在2层" 
        android:background="#FFFFEE00"
        android:layout_gravity="center"
        android:layout_width="200sp"
        android:layout_height="200sp"/>
    
</FrameLayout>


相对布局:

<?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:background="@drawable/background"
    android:id="@+id/mylayout"
    >
    
    <TextView
        android:id="@+id/text1"
        android:text="发现有新版本,您想现在就安装吗?"
        android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
    
    <Button
        android:id="@+id/button1" 
        android:text="现在更新"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text1"
        android:layout_toLeftOf="@+id/button2"
        />
    
    <Button 
        android:id="@+id/button2"
        android:text="以后再说"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/text1"
        android:layout_below="@+id/text1"
        />
        
    
    
</RelativeLayout>

 

转载于:https://my.oschina.net/u/1011854/blog/266156

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值