【问题标题】:Can i have the multi layout orientation for a layout?我可以为布局设置多布局方向吗?
【发布时间】:2012-06-23 04:51:54
【问题描述】:

我有一个动态布局,并在线性布局中添加组件。组件的数量也是动态的。

当线性布局方向是水平时,项目是水平添加的,而垂直项目是垂直添加的。

现在我可以先水平平铺项目,然后垂直平铺项目,就像垂直填充项目一样。

或者我可以使用任何其他布局来满足我的需要。

点赞

【问题讨论】:

标签: android android-layout android-linearlayout


【解决方案1】:

您绝对可以嵌套多个方向的LinearLayout 元素。例如:

<LinearLayout 
    android:id="@+id/main_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/firstRow"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal" >
        ...
    </LinearLayout>

    <LinearLayout
        android:id="@+id/secondRow"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal" >
        ...
    </LinearLayout>

    <LinearLayout
        android:id="@+id/thirdRow"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal" >
        ...
    </LinearLayout>
</LinearLayout>

使用这样的结构将允许您构建您描述的布局。

对于动态行,您可以执行以下操作:

layout_row.xml

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

MainActivity.java

mainLayout = (LinearLayout) findViewById(R.id.main_layout);

// for each dynamic row you can inflate a new view and add it
dynamicRow1 = getLayoutInflater.inflate(R.layout.layout_row, null);
mainLayout.addView(dynamicRow1);

dynamicRow2 = getLayoutInflater.inflate(R.layout.layout_row, null);
mainLayout.addView(dynamicRow2);

dynamicRow3 = getLayoutInflater.inflate(R.layout.layout_row, null);
mainLayout.addView(dynamicRow3);

您可以在每一行中以编程方式添加您需要使用相同类型的逻辑创建的动态视图。

【讨论】:

  • 我的布局将是动态的。所以我不能提前定义行数。
  • 这很公平。您可以轻松地创建一个只有水平方向LinearLayout 的布局文件,以编程方式扩展该布局,然后将其添加为主LinearLayout 的子级。查看代码示例的编辑。
猜你喜欢
  • 1970-01-01
  • 2012-03-02
  • 2014-05-11
  • 2016-01-28
  • 1970-01-01
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多