1

我在使用linearLayout分割屏幕时遇到了一个问题,我的目标是将它分割成9个Pisces - 当我开始使用LinearLayout时,我得到了以下结果:

http://imageshack.us/photo/my-images/4/firstna.png/

使用以下 xml 代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
    <LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:baselineAligned="false">
    <FrameLayout
      android:gravity="center_horizontal"
      android:background="#aa0000"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1">
    </FrameLayout>
    <FrameLayout
      android:gravity="center_horizontal"
      android:background="#00aa00"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="4"/>
    <FrameLayout
      android:gravity="center_horizontal"
      android:background="#aaaa00"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1"/>
    </LinearLayout>
    </LinearLayout>

后来当我尝试向左侧 FrameLayout 添加内容时,它改变了这个 FrameLayout 的比例(这是问题..):

http://imageshack.us/photo/my-images/692/secondp.png/

使用以下代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
    <LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:baselineAligned="false">
  <FrameLayout
      android:gravity="center_horizontal"
      android:background="#aa0000"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1">
      <LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
    android:text="row one"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
<TextView
    android:text="row two"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
<TextView
    android:text="row three"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
<TextView
    android:text="row four"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
    </LinearLayout>
    </FrameLayout>
    <FrameLayout
      android:gravity="center_horizontal"
      android:background="#00aa00"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="4"/>
    <FrameLayout
      android:gravity="center_horizontal"
      android:background="#aaaa00"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1"/>
    </LinearLayout>
    </LinearLayout>

我希望能够仅使用 layout_weight 属性来控制布局 - 如果将内容添加到 FrameLayout 会更改其比率,我需要您帮助找到另一个将保存该比率的容器。谢谢。

4

1 回答 1

0

哇,你使用了很多嵌套布局!你的左手布局是否需要是一个框架布局,里面有另一个线性布局,然后是所有的文本视图?或者你可以只替换 FrameLayout 父级并将背景颜色移动到垂直 LinearLayout 吗?

实际上,为什么你也需要第二个线性布局?您可以只指定第一个线性布局的方向为水平方向(无论如何您在高度上都有 fill_parent),然后删除第二个嵌套线性布局。

此外,您可能应该考虑将 FrameLayouts 的布局宽度更改为 0dp,以便它们根据权重动态调整大小。

于 2012-04-09T22:42:27.987 回答