简体   繁体   English

如何创建横向和纵向布局

[英]How to create a layout to Landscape and Portrait orientation

Please help me set Android layout using XML. 请帮助我使用XML设置Android布局。

I want to set layout orientation for portrait and landscape, but they are not responsive. 我想为纵向和横向设置布局方向,但是它们没有响应。 When I see in portrait, it looks fine, but when I see in landscape, it does not. 当我看到人像时,看起来不错,但是当我看到风景时,则看不出来。

This is my code: 这是我的代码:

<Linear Layout XML:android="HTTP://sachems.android.com/apk/res/android"
    XML:tools="HTTP://sachems.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@draw able/home"
    android:orientation="vertical">
    <Linear Layout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weight Sum="100" >
        <Linear Layout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_gravity="bottom"
            android:layout_margin Top="370dp"
            android:layout_weight="80"
            android:orientation="vertical" >
            <Button
                android:id="@+id/btn_book"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:layout_margin Left="5dp"
                android:layout_margin Right="5dp"
                android:layout_margin Top="100dp"
                android:layout_margin Bottom="10dp"
                android:background="#66FF0000"
                android:gravity="center"
                android:text="Book"
                android:text Size="20sp" />
        </Linear Layout>


    </Linear Layout>
</Linear Layout>

This looks good only for one side orientation, either portrait or landscape, in Android tablet after running the application, but not in both when check by rotating the tablet. 在运行应用程序后,这仅适用于Android平板电脑中纵向或横向的一侧方向,但旋转平板电脑进行检查时,这两种方式都不适合。

If i understood your question correctly you want to have one layout for portrait mode and second layout for horizontal. 如果我正确理解了您的问题,则您希望纵向布局采用一种布局,水平布局采用另一种布局。 To do that you will need two separate XML files with the same name placed in layout-land and layout-port folder. 为此,您需要将两个具有相同名称的单独XML文件放置在layout-landlayout-port文件夹中。

http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources

If you want to make another layout for landscape then put it in 如果要为景观进行其他布局,则将其放入

res -> layout-land folder . res-> layout-land文件夹。

Both the names of the xml are must be same for which is used for portrait and landscape . xml的两个名称必须相同(用于纵向和横向)。

try this 尝试这个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="HTTP://sachems.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ddd"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="100">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_gravity="bottom"
            android:layout_weight="95"
            android:orientation="vertical">

        </LinearLayout>

        <Button
            android:id="@+id/btn_book"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="5"
            android:background="#66FF0000"
            android:gravity="center"
            android:text="Book"
            android:textSize="20sp" />
    </LinearLayout>
</LinearLayout>

this way the book button will always be there at the bottom. 这样,书本按钮将始终位于底部。 Is that what u are looking for? 那是你要找的吗?

Create same name layout in rec=>layout rec=>layout创建相同的名称rec=>layout

Create new directory layout-land 创建新的目录layout-land

在此处输入图片说明

And add xml file in layout-land directory 并在layout-land目录中添加xml文件

** main.xml** ** main.xml **

<LinearLayout XML:android="HTTP://sachems.android.com/apk/res/android"
XML:tools="HTTP://sachems.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@draw able/home"
android:orientation=horizontal">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weight Sum="100" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="bottom"
        android:layout_margin Top="370dp"
        android:layout_weight="80"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/btn_book"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_margin Left="5dp"
            android:layout_margin Right="5dp"
            android:layout_margin Top="100dp"
            android:layout_margin Bottom="10dp"
            android:background="#66FF0000"
            android:gravity="center"
            android:text="Book"
            android:text Size="20sp" />
    </LinearLayout>
 </LinearLayout>
</LinearLayout>

And out of Layout-land directory is same name xml but orientation is change xml Layout-land目录中,名称是xml,但orientation是xml

main.xml main.xml

  <LinearLayout XML:android="HTTP://sachems.android.com/apk/res/android"
XML:tools="HTTP://sachems.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@draw able/home"
android:orientation="virtical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="virtical"
    android:weight Sum="100" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="bottom"
        android:layout_margin Top="370dp"
        android:layout_weight="80"
        android:orientation="virtical" >
        <Button
            android:id="@+id/btn_book"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_margin Left="5dp"
            android:layout_margin Right="5dp"
            android:layout_margin Top="100dp"
            android:layout_margin Bottom="10dp"
            android:background="#66FF0000"
            android:gravity="center"
            android:text="Book"
            android:text Size="20sp" />
    </LinearLayout>
 </LinearLayout>
</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM