如何在Android中的片段中添加持久性底部工作表

时间:2016-12-25 16:46:59

标签: android android-fragments

我想在我的片段中添加持久性底部工作表。我有一个基本的片段代码和一个底部的表格布局。有人可以帮我整理片段里面的底片。

感谢您的时间,感谢您。

这是我的片段:

package app.com.navact;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class TabFragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tab_fragment1, container, false);
    }
}

这是我的持久性底部工作表布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mayojava.sample.bottomsheetdemo.MainActivity">

    <!-- main content layout-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">
        ......
    </RelativeLayout>

    <!-- bottom sheet layout -->
    <RelativeLayout
        android:id="@+id/linear_layout_bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        app:behavior_peekHeight="80dp"
        app:behavior_hideable="true"
        app:layout_behavior="hellooooz"
        android:elevation="20sp"
        android:background="@color/colorAccent">
        <TextView
            android:id="@+id/text_view_sheet_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="kalle kalle "
            android:textSize="16sp"
            android:padding="@dimen/activity_vertical_margin"/>

        <TextView
            android:id="@+id/text_view_more_content"
            android:text="helllo"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text_view_sheet_title"
            android:paddingLeft="@dimen/activity_vertical_margin"
            android:paddingRight="@dimen/activity_vertical_margin"/>

        <Button
            android:text="balle balle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="@dimen/activity_vertical_margin"
            android:layout_marginRight="@dimen/activity_vertical_margin"
            android:layout_marginTop="@dimen/activity_vertical_margin" android:layout_below="@+id/text_view_more_content"/>

    </RelativeLayout>

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

2 个答案:

答案 0 :(得分:1)

在Fragment layout xml中,将底部工作表的RelativeLayout的布局行为更改为:

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"

答案 1 :(得分:0)

将最新的appcompat和设计支持库添加到您的项目中。 在您的build.gradle

dependencies {
    //replace X.X.X with the latest version
    compile 'com.android.support:appcompat-v7:X.X.X'
    compile 'com.android.support:design:X.X.X'
}

您的底部工作表布局应变为:

<RelativeLayout
    android:id="@+id/linear_layout_bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="220dp"
    app:behavior_peekHeight="80dp"
    app:behavior_hideable="true"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:elevation="20sp"
    android:background="@color/colorAccent">

    <TextView
        android:id="@+id/text_view_sheet_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="kalle kalle "
        android:textSize="16sp"
        android:padding="@dimen/activity_vertical_margin"/>

    <TextView
        android:id="@+id/text_view_more_content"
        android:text="helllo"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text_view_sheet_title"
        android:paddingLeft="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_vertical_margin"/>

    <Button
        android:text="balle balle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="@dimen/activity_vertical_margin"
        android:layout_marginRight="@dimen/activity_vertical_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin" android:layout_below="@+id/text_view_more_content"/>

</RelativeLayout>

然后在您的片段中:

RelativeLayout bottomSheetLayout = view.findViewById(R.id.linear_layout_bottom_sheet);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetLayout);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);

我希望这会有所帮助。