簡談Android的布局

來源:互聯網
上載者:User

首先明確一下,我要講的不是LinearLayout,ScrollView...等布局。我想簡單說一下橫屏和豎屏

我們可能會遭遇到一下問題,比如我前面給出的小Q單位轉換器。不知道有沒有網友試一下,下面我們來試一下,我的main.xml是這樣寫的

<?xml version="1.0" encoding="utf-8" ?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br />android:id="@+id/ad_container"<br />android:orientation="vertical"<br />android:layout_width="fill_parent"<br />android:layout_height="wrap_content" ><br /><TextView<br />android:id="@+id/item1"<br />android:layout_width="fill_parent"<br />android:layout_height="wrap_content"<br />/><br /><EditText<br />android:id="@+id/edittext1"<br />android:layout_width="fill_parent"<br />android:layout_height="wrap_content"<br />android:numeric="decimal" android:text="" /><br /><TextView<br />android:id="@+id/item2"<br />android:layout_width="fill_parent"<br />android:layout_height="wrap_content"<br />/><br /><EditText<br />android:id="@+id/edittext2"<br />android:layout_width="fill_parent"<br />android:layout_height="wrap_content"<br />android:numeric="decimal"<br />android:text="" /><br /><TextView<br />android:id="@+id/item3"<br />android:layout_width="fill_parent"<br />android:layout_height="wrap_content"<br /> /><br /> <EditText<br /> android:id="@+id/edittext3"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:numeric="decimal"<br /> android:text="" /><br /> <Button<br /> android:id="@+id/change"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="轉換" /><br /> <Spinner<br /> android:id="@+id/Choice"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content" /><br /> </LinearLayout>

這樣寫完後我們運行程式,得到的介面是這樣的:

有人要說了,這沒問題啊,除了醜點,這不是沒問題嗎?別急,按一下Ctrl+F12(真機直接橫過來就行了吧?)發現了沒?出問題了沒?

 

這時候我們可以看到spinner組件已經不能很好的顯示出來,當然我現在還可以用滑鼠點,手指?我就不確定了......所以我們要迫切的想一種辦法來解決問題。

很多國內的Android開發書籍都沒有講解這方面的知識,最後在一本很薄很給力的書上看到了一個解決方案:在res檔案夾下添加一個layout-land檔案夾(一定要注意尾碼是-land),在該目錄下添加main.xml然後重新布局,也就是設計一種可以再橫屏下正常顯示的布局。

新布局代碼如下:

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"><br /> <LinearLayout<br /> android:orientation="horizontal"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> ><br /> <TextView<br />android:id="@+id/item1"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> /><br /><EditText<br />android:id="@+id/edittext1"<br />android:layout_width="fill_parent"<br />android:layout_height="wrap_content"<br />android:numeric="decimal"<br />/><br /> </LinearLayout></p><p><LinearLayout<br /> android:orientation="horizontal"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content" ><br /><TextView<br />android:id="@+id/item2"<br />android:layout_width="wrap_content"<br />android:layout_height="wrap_content"<br />/><br /><EditText<br />android:id="@+id/edittext2"<br />android:layout_width="fill_parent"<br />android:layout_height="wrap_content"<br />android:numeric="decimal"<br />/><br /></LinearLayout><br /><LinearLayout<br /> android:orientation="horizontal"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content" ><br /><TextView<br />android:id="@+id/item3"<br />android:layout_width="wrap_content"<br />android:layout_height="wrap_content"<br />/><br /><EditText<br />android:id="@+id/edittext3"<br />android:layout_width="fill_parent"<br />android:layout_height="wrap_content"<br />android:numeric="decimal"<br />/><br /></LinearLayout><br /><LinearLayout<br /> android:orientation="horizontal"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content" ><br /><Button<br />android:id="@+id/change"<br />android:layout_width="wrap_content"<br />android:layout_height="wrap_content"<br />/><br /><Spinner<br />android:id="@+id/Choice"<br />android:layout_width="fill_parent"<br />android:layout_height="wrap_content"<br />/><br /></LinearLayout><br /></LinearLayout>

我們再來運行一下:

我們看到,現在可以放下原來所有的組件,介面也不是特別醜,我沒有加任何文字和圖片,稍微設計一下還是看的過去的。當然我只是做一個樣本,解決方案有很多種,發揮你的創意吧!記得尾碼是-land。。。。。。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.