【问题标题】:How to place a layout at fixed position in tabs layout?如何在选项卡布局中将布局放置在固定位置?
【发布时间】:2018-07-21 09:51:35
【问题描述】:

我有一个选项卡活动,其中有 3 个选项卡。一切正常,但我想在片段底部添加一个按钮,该按钮应固定在 FAB 之类的位置。按钮布局在底部但只有当我向下滚动片段时。我想要的是按钮布局应该固定在一个位置,即使它上面的recyclerview被滚动。如何实现它,有人可以帮忙吗? 我的xml代码

<?xml version="1.0" encoding="utf-8"?>
<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">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_messages"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/padd_5"
    android:layout_weight="1" />

<LinearLayout
    android:id="@+id/replyLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/padd_2"
    android:layout_weight="0"
    android:background="@drawable/custom_border"
    android:padding="@dimen/padd_5">

    <EditText
        android:id="@+id/edt_reply"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/write_a_comment"
        android:textColor="@color/grey"
        android:textSize="15sp" />

    <Button
        android:id="@+id/btn_reply"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:background="@color/colorAccent"
        android:text="@string/reply"
        android:textAllCaps="false"
        android:textColor="@color/white" />


</LinearLayout>

</LinearLayout>

回复布局正在显示,但是当我向上滚动时

我想要的是,回复布局应该固定在一个位置,recyclerview内容应该在它后面。回复布局应该固定在突出显示的位置

【问题讨论】:

  • 您想在 recyclerview 下方或 recyclerview 上方添加按钮?
  • @HemantParmar 在回收站视图上
  • 可以,而不是使用 RelativeLayout 而不是线性。
  • 如果我使用相对布局,那么编辑文本会变得透明,这是我不想要的

标签: android android-layout android-linearlayout


【解决方案1】:

试试这个

    <?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_messages"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/padd_5"
        android:layout_weight="1" />

    <LinearLayout
        android:id="@+id/replyLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_margin="@dimen/padd_2"
        android:layout_weight="0"
        android:background="@drawable/custom_border"
        android:padding="@dimen/padd_5">

        <EditText
            android:id="@+id/edt_reply"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="@string/write_a_comment"
            android:textColor="@color/grey"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btn_reply"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:background="@color/colorAccent"
            android:text="@string/reply"
            android:textAllCaps="false"
            android:textColor="@color/white" />


    </LinearLayout>

</RelativeLayout>

【讨论】:

  • 它显示了 edittext 的透明布局。我想要的是它应该与 recyclerview 重叠
  • 它与您发布的相同 XML 我只是将父级更改为 Relative 并将 android:layout_alignParentBottom="true" 添加到您的 replyLayout
  • 如果你想要 replyLayout 顶部的 RecyclerView 只需将 android:layout_alignParentBottom="true" 替换为 android:layout_alignParentTop="true"replyLayout 并将 android:layout_below="@+id/replyLayout" 添加到您的 RecyclerView
【解决方案2】:

这样做:

<?xml version="1.0" encoding="utf-8"?>
<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">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_messages"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="@dimen/padd_5"
    android:layout_weight="1" />

<LinearLayout
    android:id="@+id/replyLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/padd_2"
    android:background="@drawable/custom_border"
    android:padding="@dimen/padd_5">

    <EditText
        android:id="@+id/edt_reply"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/write_a_comment"
        android:textColor="@color/grey"
        android:textSize="15sp" />

    <Button
        android:id="@+id/btn_reply"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:background="@color/colorAccent"
        android:text="@string/reply"
        android:textAllCaps="false"
        android:textColor="@color/white" />
</LinearLayout>
</LinearLayout>

【讨论】:

  • @Neha 你需要在回收站视图下方显示固定在底部的编辑文本布局吗?它在我的最后工作。在我的回答中,您的代码只有一处更改。高度 0dp。
【解决方案3】:

请试试这个,我做了一些改变。 1.增加滚动视图 2.改变布局权重 3.线性布局到相对布局。 4. alignParent Bottom=true

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="wrap_content">
   <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_messages"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/padd_5"
        android:layout_weight="1" />
</ScrollView>

<RelativeLayout
android:id="@+id/replyLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padd_2"
android:layout_alignParentBottom="true"
android:background="@drawable/custom_border"
android:padding="@dimen/padd_5">

<EditText
    android:id="@+id/edt_reply"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="5"
    android:hint="@string/write_a_comment"
    android:textColor="@color/grey"
    android:textSize="15sp" />

<Button
    android:id="@+id/btn_reply"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:layout_weight="3.5"
    android:layout_gravity="center"
    android:background="@color/colorAccent"
    android:text="@string/reply"
    android:textAllCaps="false"
    android:textColor="@color/white" />


</RelativeLayout>

</LinearLayout>

【讨论】:

    【解决方案4】:

    这可以使用 ConstraintLayout 轻松完成。

    试试这个代码。

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_messages"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="@dimen/padd_5"
            app:layout_constraintBottom_toTopOf="@+id/replyLayout"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />
    
        <LinearLayout
            android:id="@+id/replyLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/padd_5"
            android:padding="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">
    
            <EditText
                android:id="@+id/edt_reply"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="Comment"
                android:textColor="@color/grey"
                android:textSize="15sp" />
    
            <Button
                android:id="@+id/btn_reply"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_gravity="center"
                android:background="@color/colorAccent"
                android:text="Reply"
                android:textAllCaps="false"
                android:textColor="@android:color/white" />
        </LinearLayout>
    </android.support.constraint.ConstraintLayout>
    

    即使在 recyclerview 向上/向下滚动时,此限制回复布局也保持在底部。 顶部区域为 recyclerview 填充消息,底部区域用于回复您使用 LinearLayout 创建的布局。

    【讨论】:

    • 记得将实现 'com.android.support.constraint:constraint-layout:1.1.2' 添加到您的应用 gradle 模块中。
    • 谢谢你的回答,但是当我滚动屏幕时它仍然显示回复布局,它应该固定在一个地方。如果你知道请帮忙
    • 这种奇怪的行为是由 'CoordinatorLayout app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"' 引起的。它用于在用户向下滚动时隐藏操作栏并在其位置移动选项卡。
    • Nilesh 提到的一个解决方案是将回复布局添加到活动中。或者您可以将布局添加到顶部,这样它就会保持在顶部,但这不是一个好的设计。
    • @ArslanShoukat 是的,你是对的。谢谢你的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多