【问题标题】:How to create a layout to Landscape and Portrait orientation如何创建横向和纵向的布局
【发布时间】:2015-10-29 11:50:38
【问题描述】:

请帮助我使用 XML 设置 Android 布局。

我想为纵向和横向设置布局方向,但它们没有响应。纵向看还不错,横向看就不行了。

这是我的代码:

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

这仅适用于运行应用程序后在 Android 平板电脑中的一侧方向(纵向或横向),但在通过旋转平板电脑进行检查时不适用于两者。

【问题讨论】:

  • 你想只在纵向模式下运行还是两者都运行?
  • Linear Layout错误并且不会编译。请改用LinearLayout。另外,请避免嵌套布局。为了更好的表现。

标签: android xml


【解决方案1】:

如果我正确理解了您的问题,您希望为纵向模式设置一种布局,为水平模式设置第二种布局。为此,您需要将两个具有相同名称的单独 XML 文件放在 layout-landlayout-port 文件夹中。

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

【讨论】:

    【解决方案2】:

    如果你想为横向做另一个布局,那就把它放进去

    res -> layout-land 文件夹。

    xml 的名称必须相同,用于纵向和横向。

    【讨论】:

    • 我现在还能使用layout-land文件夹吗?
    【解决方案3】:

    试试这个

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

    这样,书本按钮将始终位于底部。这就是你要找的吗?

    【讨论】:

    • 如果有帮助请告诉我
    【解决方案4】:

    rec=&gt;layout中创建同名布局

    创建新目录layout-land

    并在layout-land目录下添加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>
    

    Layout-land 目录中的xml 名称相同,但orientation 是更改xml

    ma​​in.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>
    

    【讨论】:

      猜你喜欢
      • 2012-09-25
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多