安卓開發 各類佈局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> android


水平線性佈局: 佈局

<?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> xml


表格佈局的登陸頁面 utf-8

注意:android:stretchColumns="0,3"容許第一列和第四列伸縮,以配合爲左右居中顯示 input

<?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> it


幀佈局: io

<?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> 登錄


相對佈局: coding

<?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> layout

相關文章
相關標籤/搜索