安卓的主要幾大佈局

来源:http://www.cnblogs.com/wuziyue/archive/2016/04/09/5371925.html
-Advertisement-
Play Games

今天我們的主要內容就是安卓的主要幾個基礎的佈局方式。(主要佈局如下:) 1.線性佈局(LinerLayout) 2.相對佈局(RelativeLayout) 3.表格佈局(TableLayout) 4.網格佈局(GridLayout) 5.絕對佈局(AbsoluteLayout) 6.幀佈局(Fra ...


今天我們的主要內容就是安卓的主要幾個基礎的佈局方式。(主要佈局如下:)

1.線性佈局(LinerLayout)

2.相對佈局(RelativeLayout)

3.表格佈局(TableLayout)

4.網格佈局(GridLayout)

5.絕對佈局(AbsoluteLayout)

6.幀佈局(FrameLayout)

一:線性佈局(LinerLayout)。

1.xml文件配置:

<?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="5dp" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#0000FF"
        android:gravity="center_horizontal|center_vertical"
        android:text="線性佈局"
        android:textSize="20sp"/>

<LinearLayout
    android:layout_width="200dp"
    android:layout_height="150dp"
    android:layout_gravity="center_horizontal|center_vertical"
    android:background="#0000FF"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="#00FF00"
        android:text="內容一" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#F7F709"
        android:text="內容二" />

</LinearLayout>
<LinearLayout
    android:layout_width="200dp"
    android:layout_height="150dp"
    android:layout_gravity="right"
    android:background="#0000FF"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#00FF00"
        android:text="內容一" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="#F7F709"
        android:text="內容二" />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
 <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="按一" 
        android:onClick="click"
        />
  <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="按二" 
        android:onClick="click"/>
   <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="按三" 
        android:onClick="click"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="按四" 
        android:onClick="click"/>

</LinearLayout>
</LinearLayout>

展示:

2.xml文件配置:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center_horizontal|center_vertical"
        android:text="線性佈局登錄頁面"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_marginTop="20dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="賬號:"
            android:textSize="15sp" />

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="密碼:"
            android:textSize="15sp" />

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="30dp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_marginTop="15dp"
        android:orientation="horizontal" >

        <CheckBox
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="記住賬號" />

        <CheckBox
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="記住密碼" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="30dp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_marginTop="15dp"
        android:orientation="horizontal" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="click"
            android:text="登錄"
            android:textSize="10sp" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="15sp" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="click"
            android:text="註冊"
            android:textSize="10sp" />
    </LinearLayout>

</LinearLayout>

展示:

二:相對佈局(RelativeLayout)

1.xml文件佈局如下:

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="相對佈局登錄頁面"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView"
        android:layout_marginLeft="80px"
        android:layout_marginTop="30dp"
        android:text="賬號:"
        android:textSize="15sp" />

    <EditText
        android:id="@+id/firstEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/textView1"
        android:layout_marginLeft="130px"
        android:layout_marginRight="50px" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/firstEditText"
        android:layout_marginLeft="80px"
        android:layout_marginTop="20dp"
        android:text="密碼:"
        android:textSize="15sp" />

    <EditText
        android:id="@+id/firstEditText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/textView2"
        android:layout_marginLeft="130px"
        android:layout_marginRight="50px" />

    <CheckBox
        android:id="@+id/checkbox1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/firstEditText2"
        android:layout_marginLeft="100px"
        android:layout_marginTop="20dp"
        android:text="記住賬號" />

    <CheckBox
        android:id="@+id/checkbox2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/checkbox1"
        android:layout_marginLeft="280px"
        android:layout_marginRight="50px"
        android:text="記住密碼" />

    <Button
        android:id="@+id/cancelButton1"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/checkbox2"
        android:layout_marginLeft="100px"
        android:layout_marginRight="290px"
        android:layout_marginTop="25dp"
        android:text="登錄"
        android:textSize="14sp" />

    <Button
        android:id="@+id/confremButton2"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignBottom="@id/cancelButton1"
        android:layout_marginLeft="290px"
        android:layout_marginRight="100px"
        android:text="註冊"
        android:textSize="14sp" />

</RelativeLayout>

展示:

三:表格佈局(TableLayout)

xml文件如下:

<?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="5dp"
    android:stretchColumns="2" >

    <TableRow>

        <TextView
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="序號"
            android:textSize="20sp" />

        <TextView
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="書名"
            android:textSize="20sp" />

        <TextView
            android:gravity="center"
            android:text="內容"
            android:textSize="20sp" />

        <TextView
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="作者"
            android:textSize="20sp" />
    </TableRow>

    <View
        android:layout_height="1.5dp"
        android:background="#00FF00" />

    <TableRow>

        <TextView
            android:gravity="center"
            android:text="1" />

        <TextView
            android:gravity="center"
            android:text="西游記" />

        <TextView
            android:gravity="center"
            android:text="取經" />

        <TextView
            android:gravity="center"
            android:text="鄭晨" />
    </TableRow>

    <View
        android:layout_height="1.5dp"
        android:background="#00FF00" />

    <TableRow>

        <TextView
            android:gravity="center"
            android:text="2" />

        <TextView
            android:layout_column="2"
            android:gravity="center"
            android:text="孫悟空大鬧天空" />
    </TableRow>

    <View
        android:layout_height="1.5dp"
        android:background="#00FF00" />

    <TableRow>

        <TextView
            android:gravity="center"
            android:text="3" />

        <TextView
            android:layout_column="1"
            android:gravity="center"
            android:text="盤龍" />

        <TextView
            android:layout_column="3"
            android:gravity="center"
            android:text="鄭晨" />
    </TableRow>

    <View
        android:layout_height="1.5dp"
        android:background="#00FF00" />

</TableLayout>

展示:

四:網格佈局(GridLayout)

1.xml文件如下:

<?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="5dp" >
<!-- 網格佈局 -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:text="電腦"
        android:textSize="30sp" />

    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:columnCount="4"
        android:rowCount="6" >

        <EditText
             android:id="@+id/textView6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_columnSpan="4"
            android:layout_gravity="fill_horizontal"
            android:editable="false"
            android:focusable="false"
            android:gravity="right"
            android:text="0"
            android:textSize="20sp" />

        <Button android:text="AC" 
            android:onClick="acClick"/>

        <Button android:text="+/-" />

        <Button android:text="%" />

        <Button android:text="+" />

        <Button android:text="7" />

        <Button android:text="8" />

        <Button android:text="9" />

        <Button android:text="x" 
            android:onClick="anClick" />

        <Button android:text="4" />

        <Button android:text="5" />

        <Button android:text="6" 
            android:onClick="bnClick" />

        <Button android:text="-" />

        <Button android:text="1"
            android:onClick="onClick" />

        <Button android:text="2" />

        <Button android:text="3" />

        <Button android:text="+" />

        <Button
            android:layout_columnSpan="2"
            android:layout_gravity="fill_horizontal"
            android:text="0" />

        <Button android:text="." />

        <Button android:text="=" />
    </GridLayout>

</LinearLayout>

2.Activity如下(這是我只寫了幾個按鍵可以按,作為例子):

//網格佈局
public class GridLayout extends Activity {
    private EditText editText;
    private boolean lastClickIsNumber = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);
        editText = (EditText) findViewById(R.id.textView6);

    }

    public void acClick(View v) {

    }

    public void onClick(View v) {
        if (!lastClickIsNumber) {
            editText.setText("1");

        } else {
            String oldContent = editText.getText().toString().trim();
            oldContent += "1";
            editText.setText(oldContent);

        }
        lastClickIsNumber = true;
    }

    public void anClick(View v) {
        if (!lastClickIsNumber) {
            editText.setText("x");

        } else {
            String oldContent = editText.getText().toString().trim();
            oldContent += "x";
            editText.setText(oldContent);

        }
        lastClickIsNumber = true;
    }

    public void bnClick(View v) {
        if (!lastClickIsNumber) {
            editText.setText("6");

        } else {
            String oldContent = editText.getText().toString().trim();
            oldContent += "6";
            editText.setText(oldContent);

        }
        lastClickIsNumber = true;
    }

}

展示:

五.絕對佈局(AbsoluteLayout)

xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:padding="5dp">
<TextView 
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_x="100dp"
    android:layout_y="50dp"
    android:text="你好嗎?"/>

<TextView 
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_x="50dp"
    android:layout_y="100dp"
    android:text="我很好!"/>

<TextView 
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_x="200dp"
    android:layout_y="100dp"
    android:text="真的嗎?"/>

<TextView 
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_x="100dp"
    android:layout_y="200dp"
    android:text="真的!"/>
</AbsoluteLayout>

展示:

 

到此結束,接下來我會介紹安卓的基本控制項。

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1.先看看網上流傳的他們的繼承圖: 從上面可以看出CABasicAnimation與CAKeyframeAnimation都繼承於CAPropertyAnimation。而CAPropertyAnimation提供了基本的動畫屬性設置keyPath、additive、cumulative。 2.詳解 ...
  • 一般的手機沒有root許可權,進不去data/data目錄,當手機刷機了後,擁有root許可權,就可以進入data/data目錄,查看我們保存的密碼文件,因此我們需要對存入的密碼進行MD5加密 獲取MessageDigest信息摘要器對象,調用MessageDigest.getInstance(“md5 ...
  • 一直以來,我們把所有的段描述符都放在GDT中,而不管它屬於內核還是用戶程式,為了有效地在任務之間實施隔離,處理器建議每個任務都應當具有自己的描述符表,稱為局部描述符表LDT,並且把專屬於自己的那些段放到LDT中。 和GDT一樣,LDT也是用來存放描述符的。不同之處在於,LDT只屬於某個任務。或者說, ...
  • 原博客地址:http://www.cnblogs.com/tianzhijiexian/p/4161811.html 相信看這篇文章的人都應該知道android中的Dialog了吧,如果對於Dialog還不是很瞭解可以看我之前的一篇詳解文章: Dialog詳解:http://www.cnblogs. ...
  • 跳轉到指定app的實現 IOS中應用的跳轉是通過URL實現的,因此在實現應用跳轉之前我們要設置一下對應的URL。 圖一(尋找配置軟體的URL) 圖二(具體配置選項) 註意: 如果IOS版本為IOS9 我們需要為app設置白名單。 實現跳轉的前提是有這個app,因此我們需要把被跳轉的app先運行,即安 ...
  • Mask屬性介紹 Mask平時用的最多的是masksToBounds 吧. 其實除此以外Mask使用場景很多,看完之後你會發現好真是好用的不要不要的... 先來瞭解下Mask屬性到底是什麼? Mask 英文解釋是蒙板/面罩,平時我們稱為蒙層. 在蘋果官方文檔里如下圖,意思是Mask是一個可選的Lay ...
  • 今天我們介紹的是Checkbox多選框: 1.Activity 2.xml文件如下: 3.效果圖如下: ...
  • 在移動應用滿天飛的時代,隨著移動支付的盛行,很多應用中都集成了支付功能。之前的支付一直不是我負責,近期這個項目我負責訂單模塊少不了要做支付,每每提起支付就覺得怕怕,覺得很難,但當真正做的時候卻遠遠沒有想象中的那麼難。支付寶的SDK提供了demo,我們只需要配置好一些基本信息,這個demo就可以正常支 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...