【问题标题】:Make View fill the gap in the middle but leaves space to its bottom siblings使 View 填补中间的空白,但为其底部的兄弟姐妹留出空间
【发布时间】:2016-04-07 16:14:49
【问题描述】:

嗨,我现在有点难过。我认为这在 Android XML 中是可能的。

我想做这样的布局:

问题在于并排的两个小元素。当我使 view#2 与高度匹配时,我希望它也会尊重其他兄弟的空间。我错了,但我现在明白了。当我将 view#2 的 layout_height 设置为 match_parent 时,它会占用剩余空间,并且 view#3 和 view#4 被放入最底部并且不再在屏幕中可见。

我希望 view#2 会在中间扩展,但仍然为 view#3 和 view#4 以及它下面的一些元素腾出一些空间,而不是被推到下面。

这是我现在正在做的事情:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:fitsSystemWindows="true"
    tools:context=".TaskActivity">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textinput_layout_note_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="16dp">

        <EditText
            android:id="@+id/edittext_task"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:singleLine="true"
            android:textSize="32sp"
            android:padding="8dp"
            android:hint="@string/edittext_task_title_hint"/>

    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textinput_layout_note_description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textinput_layout_note_title">

        <EditText
            android:id="@+id/edittext_todo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:maxLines="1"
            android:gravity="top"
            android:hint="@string/edittext_task_content_hint"/>

    </android.support.design.widget.TextInputLayout>

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

        <ImageButton
            android:id="@+id/imagebutton_insert_note"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_content_add_black"
            android:contentDescription="@string/content_description_choose_note"
            style="?android:attr/borderlessButtonStyle" />

        <ImageButton
            android:id="@+id/imagebutton_expand_note"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_open_in_new_black_24dp"
            android:contentDescription="@string/content_description_expand_note"
            style="?android:attr/borderlessButtonStyle" />

    </LinearLayout>

</RelativeLayout>

我已经将 LinearLayout 与 layout_weight 结合使用,但这会变得越来越慢,因为我有一些也需要权重的嵌套视图。我希望这是相对完成的。

有什么想法可以实现这种布局吗?

谢谢!

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    我希望这是相对完成的。

    RelativeLayout 不是性能的灵丹妙药。

    有什么想法可以实现这种布局吗?

    试试:

    <RelativeLayout
        ...>
    
        <android.support.design.widget.TextInputLayout
            android:id="@+id/textinput_layout_note_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:paddingTop="16dp">
    
            ...
    
        </android.support.design.widget.TextInputLayout>
    
        <android.support.design.widget.TextInputLayout
            android:id="@+id/textinput_layout_note_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/textinput_layout_note_title"
            android:layout_above="@+id/whatever_this_thing_is"/>
    
           ...
    
        </android.support.design.widget.TextInputLayout>
    
        <LinearLayout
            android:id="@id/whatever_this_thing_is"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal">
    
           ...
    
        </LinearLayout>
    
    </RelativeLayout>
    

    ... 位用于您已有的东西,已删除以保持列表更短)

    IOW:

    • 将顶视图锚定到顶部
    • 将底部视图锚定到底部
    • 将中间视图锚定在顶部下方和底部上方,使其拉伸以填充中间空间

    【讨论】:

    • 嗨@CommonsWare,如果我在视图 3 和 4 下方有其他视图怎么办。我最近一直在尝试我的,但没有让它工作。我能想到的最好方法是通过线性布局将所有内容分成两部分。但是我很好奇这对于RelativeLayout是否可行?
    • @NeonWarge: 即兴发挥,让他们成为android:layout_alignParentBottom="true"。让我识别为whatever_this_thing_isLinearLayout 拥有android:layout_above 新的东西。其他一切都将保持原样。
    【解决方案2】:

    试试这个方法!,我没有在android studio中尝试过,但它应该可以解决问题。

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <View
            android:id="@+id/viewTop"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:layout_alignParentTop="true"/>
    
        <View
            android:id="@+id/viewMiddle"
            android:layout_above="@id/viewBottom"
            android:layout_below="@id/viewTop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
        <LinearLayout
            android:id="@+id/viewBottom"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:layout_alignParentBottom="true">
    
            <View
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="50"/>
    
            <View
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="50"/>
    
        </LinearLayout>
    
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 2021-01-31
      • 1970-01-01
      • 2019-09-28
      • 2020-01-31
      • 2019-05-06
      • 2015-02-02
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多