【问题标题】:Two Relative Layouts on the one screen一个屏幕上的两个相对布局
【发布时间】:2012-09-25 10:56:08
【问题描述】:

我想在我的屏幕上有 2 个相对布局。一个低于另一个。我制作了这段代码,但它不起作用。在这种情况下,我只能看到两者的上部相对布局,尽管我设置了 wrap_content,但此布局占用了整个空间。

编辑!:我把第二个代码下面也失败了。

EDIT 2!:我发现了问题……这有点……难过。问题在于我的主题。我将背景设置为太大的 png 文件,当我通过清单应用默认主题时,这发生了。当我删除它时,一切都很好。

这个版本也失败了。

新代码:

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

    <RelativeLayout 
        android:id="@+id/some_id" 
        android:layout_above="@+id/some_id2"
        android:layout_alignParentTop="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button 
            android:id="@+id/main_button_localize" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="@string/main_localize" />

    </RelativeLayout>

    <RelativeLayout 
        android:id="@id/some_id2" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true"  >

        <ToggleButton 
            android:id="@+id/main_help" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"  />

    </RelativeLayout>

</RelativeLayout>

旧代码:

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

    <RelativeLayout
        android:id="@+id/maps_manager_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_alignParentTop="true" >

        <!-- Top -->
        <Button 
            android:id="@+id/maps_manager_top_left" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:contentDescription="@string/empty" />

        <Button 
            android:id="@+id/maps_manager_top_middle" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/maps_manager_top_left"
            android:contentDescription="@string/empty" />

        <Button 
            android:id="@+id/maps_manager_top_right" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/maps_manager_top_middle"
            android:contentDescription="@string/empty" />

        <!-- Middle -->
        <Button 
            android:id="@+id/maps_manager_middle_left" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/maps_manager_top_left"
            android:contentDescription="@string/empty" />

        <Button 
            android:id="@+id/maps_manager_middle_middle" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/maps_manager_middle_left"
            android:layout_below="@id/maps_manager_top_middle"
            android:contentDescription="@string/empty" />

        <Button 
            android:id="@+id/maps_manager_middle_right" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/maps_manager_middle_middle"
            android:layout_below="@id/maps_manager_top_right"
            android:contentDescription="@string/empty" />

        <!-- Bottom -->
        <Button 
            android:id="@+id/maps_manager_bottom_left" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/maps_manager_middle_left"
            android:contentDescription="@string/empty" />

        <Button 
            android:id="@+id/maps_manager_bottom_middle" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/maps_manager_bottom_left"
            android:layout_below="@id/maps_manager_middle_middle"
            android:contentDescription="@string/empty" />

        <Button 
            android:id="@+id/maps_manager_bottom_right" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/maps_manager_bottom_middle"
            android:layout_below="@id/maps_manager_middle_right"
            android:contentDescription="@string/empty" />

    </RelativeLayout>

    <RelativeLayout
           android:id="@+id/maps_manager_controls"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="true"
            android:layout_below="@id/maps_manager_menu" >

            <ToggleButton 
                android:id="@+id/maps_manager_help" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </RelativeLayout>

</RelativeLayout> 

【问题讨论】:

  • 我在 eclipse 上检查过,我可以看到两个相对布局都在另一个之下。
  • 将 match_parent 更改为 wrap_content
  • android:layout_alignBottom="true" by android:layout_alignParentBottom="true" 在第二个相对布局中
  • @RajeevNB 我的代码在您的 Eclipse 中有效吗?不变?我将 alignBottom 更改为 aliginParentBottom,什么也没发生。
  • @SardorDushamov 将匹配更改为换行,但在哪里?我想知道为什么 wrap_content 不起作用...

标签: android android-layout


【解决方案1】:

最好将具有垂直方向的 LinearLayout 作为两个子 RelativeLayouts 的 Parent。如果您可以为 RelativeLayouts 放置 80:20 的高度权重,那么它将适合整个屏幕。

【讨论】:

  • 如果没有其他答案,我会考虑这个。
【解决方案2】:
<ScrollView 
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content">

<RelativeLayout
        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" >
             <RelativeLayout
            android:id="@+id/maps_manager_menu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_alignParentTop="true" >

            <!-- Top -->
            <Button 
                android:id="@+id/maps_manager_top_left" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:contentDescription="@string/empty" />

            <Button 
                android:id="@+id/maps_manager_top_middle" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/maps_manager_top_left"
                android:contentDescription="@string/empty" />

            <Button 
                android:id="@+id/maps_manager_top_right" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/maps_manager_top_middle"
                android:contentDescription="@string/empty" />

            <!-- Middle -->
            <Button 
                android:id="@+id/maps_manager_middle_left" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/maps_manager_top_left"
                android:contentDescription="@string/empty" />

            <Button 
                android:id="@+id/maps_manager_middle_middle" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/maps_manager_middle_left"
                android:layout_below="@id/maps_manager_top_middle"
                android:contentDescription="@string/empty" />

            <Button 
                android:id="@+id/maps_manager_middle_right" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/maps_manager_middle_middle"
                android:layout_below="@id/maps_manager_top_right"
                android:contentDescription="@string/empty" />

            <!-- Bottom -->
            <Button 
                android:id="@+id/maps_manager_bottom_left" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/maps_manager_middle_left"
                android:contentDescription="@string/empty" />

            <Button 
                android:id="@+id/maps_manager_bottom_middle" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/maps_manager_bottom_left"
                android:layout_below="@id/maps_manager_middle_middle"
                android:contentDescription="@string/empty" />

            <Button 
                android:id="@+id/maps_manager_bottom_right" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/maps_manager_bottom_middle"
                android:layout_below="@id/maps_manager_middle_right"
                android:contentDescription="@string/empty" />

        </RelativeLayout>

        <RelativeLayout
               android:id="@+id/maps_manager_controls"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignBottom="true"
                android:layout_below="@id/maps_manager_menu" >

                <ToggleButton 
                    android:id="@+id/maps_manager_help" 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </RelativeLayout>
    </RelativeLayout>
 </ScrollView>

【讨论】:

  • ScrollView 只能有一个孩子:/
  • 你可以使用滚动视图作为根标签,并设置它的布局参数来包装内容
【解决方案3】:
<ScrollView 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" >

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <RelativeLayout
            android:id="@+id/maps_manager_menu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_alignParentTop="true" >

            <!-- Top -->
            <Button 
                android:id="@+id/maps_manager_top_left" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:contentDescription="@string/hello_world" />

            <Button 
                android:id="@+id/maps_manager_top_middle" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/maps_manager_top_left"
                android:contentDescription="@string/hello_world" />

            <Button 
                android:id="@+id/maps_manager_top_right" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/maps_manager_top_middle"
                android:contentDescription="@string/hello_world" />

            <!-- Middle -->
            <Button 
                android:id="@+id/maps_manager_middle_left" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/maps_manager_top_left"
                android:contentDescription="@string/hello_world" />

            <Button 
                android:id="@+id/maps_manager_middle_middle" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/maps_manager_middle_left"
                android:layout_below="@id/maps_manager_top_middle"
                android:contentDescription="@string/hello_world" />

            <Button 
                android:id="@+id/maps_manager_middle_right" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/maps_manager_middle_middle"
                android:layout_below="@id/maps_manager_top_right"
                android:contentDescription="@string/hello_world" />

            <!-- Bottom -->
            <Button 
                android:id="@+id/maps_manager_bottom_left" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/maps_manager_middle_left"
                android:contentDescription="@string/hello_world" />

            <Button 
                android:id="@+id/maps_manager_bottom_middle" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/maps_manager_bottom_left"
                android:layout_below="@id/maps_manager_middle_middle"
                android:contentDescription="@string/hello_world" />

            <Button 
                android:id="@+id/maps_manager_bottom_right" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/maps_manager_bottom_middle"
                android:layout_below="@id/maps_manager_middle_right"
                android:contentDescription="@string/hello_world" />

        </RelativeLayout>

        <RelativeLayout
               android:id="@+id/maps_manager_controls"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/maps_manager_menu" >

                <ToggleButton 
                    android:id="@+id/maps_manager_help" 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </RelativeLayout>
    </RelativeLayout>
</ScrollView>

【讨论】:

  • 几乎,现在我看到了两个亲戚,但他们每个人的尺寸都等于屏幕尺寸。这很糟糕。
【解决方案4】:


请按照以下步骤操作:

  1. 将 ID 添加到最顶层的相对布局中说:android:id="@+id/rtvlt_first"
  2. 在第二个 RelativeLayout 中删除 alignparenttop 属性。
  3. 添加 android:tobottomof="@id/rtvlt_first"...

    尝试这个....
    祝你好运!

【讨论】:

  • tobottomof 无法识别:/
猜你喜欢
  • 2011-10-26
  • 1970-01-01
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-01
相关资源
最近更新 更多