【问题标题】:Android Reusing layout in landscape and portraitAndroid 在横向和纵向重用布局
【发布时间】:2017-04-22 00:13:12
【问题描述】:

是否可以将参数传递给 android 中包含的布局?例如,我想做的是在横向模式下在水平 LinearLayout 中显示一组按钮,并使用相同的包含将 LinearLayout 的方向更改为“垂直”。

<include layout="buttons.xml" orientation="horizontal"/>

在用于纵向模式的布局 XML 中,我想做:

<include layout="buttons.xml" orientation="vertical"/>

在buttons.xml中我会:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doSomething"
        android:text="foo" >
    </Button>


    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doSomethingElse"
        android:text="bar" >
    </Button>

如果我正确解释了文档,则只能覆盖包含的 layout* 属性。

是否有其他方法/解决方法可以在不复制布局 xml 的情况下做到这一点? 感谢您的投入!

【问题讨论】:

    标签: android android-layout landscape-portrait


    【解决方案1】:

    Android 通过为不同的配置提供不同的文件夹来解决这个问题。

    来自文档:

    作为另一个例子,这是一个具有横向布局的替代布局的项目:

    我的项目/

    分辨率/ 布局/

           main.xml
    
       layout-land/
    
           main.xml
    

    默认情况下,layout/main.xml 文件用于纵向。

    我相信您应该这样做,因为这是文档中推荐的方式,但如果您决定使用control orientation changes,您可以自行更改方向。

    并使用它来改变布局方向:

    LinearLayout layout = (get layout from view);
    layout.setOrientation(LinearLayout.VERTICAL);
    

    layout.setOrientation(LinearLayout.HORIZONTAL); 
    

    【讨论】:

    • 您好 Mikel,在代码中动态更改布局并不是我的想法。我正在寻找一种在 xml 中执行此操作的方法。我知道资源选择器会覆盖特定的布局资源,但我担心使用选择器总是会产生冗余资源,以后维护起来很痛苦。还有其他想法/提示吗?
    【解决方案2】:

    有点晚了,但这里有另一种不拆分布局的可能性:您也可以在 xml 文件中设置 LinearLayout 的方向值。由于方向值是整数(0:水平,1:垂直),您也可以这样使用:

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="@integer/orientation" >
    

    然后在 xml 文件中设置/覆盖这个方向值:

    dimens.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <integer name="orientation">1</integer>   <!-- vertical //-->
    </resources>
    

    dimens-land.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <integer name="orientation">0</integer>   <!-- horizontal //-->
    </resources>
    

    【讨论】:

      【解决方案3】:

      好吧,我最后所做的就是将我的布局分成 3 个部分:

      button_container.xml 只包含一个 LinearLayout 和两个额外的 include

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="**horizontal**" >
      
         <include layout="@layout/button_top_row" />
         <include layout="@layout/button_bottom_row" />
      
      </LinearLayout>
      
      • button_row_top 包含应位于顶行的按钮(在手持端口视图中)

      -button_row_bottom 包含应该放在底行的按钮(同样在手持端口视图中)

      button_container.xml 有两个版本,一个是水平方向的(用于平板电脑和面向横向的移动手持设备),一个是垂直方向的版本(用于手持端口)。 所以我设法以某种方式最大限度地减少代码重复,即使它不像我希望的那样简单。

      欢迎提供任何其他提示。 再见, 马库斯

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-21
        • 1970-01-01
        • 1970-01-01
        • 2013-08-15
        • 2011-01-12
        • 1970-01-01
        相关资源
        最近更新 更多