【问题标题】:How to make this layout in Android XML如何在 Android XML 中制作此布局
【发布时间】:2014-11-29 09:12:14
【问题描述】:

我是 Android 的新手,尝试制作这种布局:

只是我想要实现的目标: 红色容器应该占用它需要的空间,但不要像绿色容器那样缩小。如果项目太多,红色容器将是可滚动的。如果有空间,绿色容器也总是以橙色居中(如果没有,它实际上仍然居中)。

我根本不知道该怎么做 :( 。这是我的尝试:

问题是我想始终保持绿色容器的高度(minHeight 不起作用,我不明白为什么)并使绿色容器以橙色居中。我对场景 2 有问题(如图所示),此代码在第一个场景中运行良好。

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

    <ScrollView
        android:id="@+id/red_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_dark"
        android:scrollbarAlwaysDrawVerticalTrack="true">

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

            <!-- Items are here -->

        </LinearLayout>

    </ScrollView>

    <LinearLayout
        android:id="@+id/orange_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_orange_dark"
        android:gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/green_container"
            android:layout_width="100dp"
            android:background="@android:color/holo_green_dark"
            android:layout_height="50dp"
            android:orientation="horizontal">

            <!-- My content -->

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

编辑:minHeight 没有帮助:

编辑:用户非法参数的图像:

【问题讨论】:

  • 需要支持2.3 android版本吗?
  • 这可以通过相对布局轻松实现
  • 红色视图中的“项目”是什么?
  • @Fox in socks :我不需要 2.3 支持。 4.0+
  • @Squonk : LinearLayouts 但理论上它可以是任何东西

标签: android layout scrollbar center


【解决方案1】:

尝试将属性 android:minHeight = "(whatever)dp" 设置为 orangeContainer。这在其他“类似”案例中对我有用。

:)

【讨论】:

  • 这对我不起作用。我已在帖子中添加了证据,请查看。
  • 我会尝试将您的顶部 LinearLayout 更改为 RelativeLayout,并使用 android:layout_below="xxx" 来放置组件。你懂我的意思吗?。尽管如此,50dp 的 minHeight 是非常小的。例如,请检查 200dp 以获得证明。
  • 不起作用。我试了百万次。如果您提供示例代码,我将不胜感激。对不起,小图片,我下次会上传更好的图片:)。
  • 我已经为此工作了“很长时间”,但恐怕我不会按照这种方式找到解决方案。问题是橙色和红色容器需要相互引用,而这个引用在 XML 解析器(XML 的渲染器)检测到它之前不存在。这就是为什么您可以引用上面声明的元素,但不能引用下面的元素。我建议您尝试使用 ListViews(作为您的红色容器)。我认为那会更容易。很抱歉不能更好地帮助您。 :(
  • 无论如何,非常感谢您抽出宝贵的时间;)。我宁愿稍微改变我的设计。我认为这是一个有趣的问题,因为它很简单,但似乎无法解决。
【解决方案2】:

这是你要找的东西吗:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/list_items"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@android:color/background_dark" >

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

<LinearLayout
    android:id="@+id/list_items"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="@android:color/darker_gray"
    android:gravity="center"
    android:minHeight="200dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

</RelativeLayout>

日食中的快照:

【讨论】:

  • 谢谢,但这根本不是我想要的。如果有空的上部(红色,在你的情况下是黑色)容器橙色(在你的情况下是灰色)容器应该占据所有空间。如果红色(在您的情况下为黑色)容器有很多,如果橙色容器(在您的情况下为灰色)中不应该有垂直空间,但橙色容器的内容必须完全可见。看看我在帖子中绘制的示例。
  • 我已经在我的帖子中为你添加了带有描述的图片。
猜你喜欢
  • 1970-01-01
  • 2012-08-03
  • 2021-10-13
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-13
相关资源
最近更新 更多