FAB не отображается с помощью CoordinatorLayout

Я не знаю почему, раньше это работало, но теперь мой FAB не виден в моем приложении. Я использовал пример галереи для создания этой активности (scrollingActivity), но я изменил NestedScrollView на LinearLayout. Я не знаю, причина ли это.

Теперь я просто хочу, чтобы мой FAB был на виду, но я не знаю, как это сделать.

Просмотр в Designer: https://nsa39.casimages.com/img/2018/05/01/180501013742949505.png

Просмотр во время выполнения: https://nsa39.casimages.com/img/2018/05/01/180501013833801844.png

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="com.grenoble.miage.miagemytag.ArretActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:titleTextAppearance="@style/Toolbar.TitleText"/>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    <include layout="@layout/content_arret" />
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:elevation="3dp"
        app:layout_anchor="@+id/app_bar"
        app:layout_anchorGravity="right|bottom"
        android:layout_gravity="end|bottom"
        app:srcCompat="@android:drawable/ic_popup_reminder"
        tools:background="@android:color/holo_orange_light" />
</android.support.design.widget.CoordinatorLayout>

включить макет content_arret

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.grenoble.miage.miagemytag.ArretActivity"
    tools:showIn="@layout/activity_arret"
    android:layout_margin="16dp"
    android:orientation="vertical">
        <GridView
            android:id="@+id/nextTimeGridHeader"
            android:layout_width="match_parent"
            android:layout_height="36dp"
            android:layout_marginBottom="10dp"
            android:numColumns="2"
            android:stretchMode="columnWidth"
            android:clickable="false"/>
    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0,1">
        <ProgressBar
            android:id="@+id/progressBar_arret"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" />
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <GridView
            android:id="@+id/nextTimeGridContent1"
            android:layout_width="170dp"
            android:layout_height="match_parent"
            android:clickable="false"
            android:numColumns="1"
            android:verticalSpacing="2dp"
            android:layout_column="0"/>
            <GridView
                android:id="@+id/nextTimeGridContent2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:numColumns="1"
                android:verticalSpacing="2dp"
                android:clickable="false"
                android:layout_column="1"/>
        </TableRow>
    </TableLayout>
</LinearLayout>

ЯВА

package com.grenoble.miage.miagemytag;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.GridView;
import android.widget.ProgressBar;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import API.RetrofitClient;
import API.TAG;
import API.model.ArretHoraire;
import API.model.Delay;
import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ArretActivity extends AppCompatActivity {
    @BindView(R.id.nextTimeGridHeader)
    GridView nextTimeGridHeader;
    @BindView(R.id.nextTimeGridContent1)
    GridView nextTimeGridContent1;
    @BindView(R.id.nextTimeGridContent2)
    GridView nextTimeGridContent2;
    @BindView(R.id.progressBar_arret)
    ProgressBar spinner;
    @BindView(R.id.fab)
    FloatingActionButton floatingActionButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_arret);
        ButterKnife.bind(this);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        final Intent intent = getIntent();
        toolbar.setTitle(intent.getStringExtra("stopName"));
        String[] destinations = new String[]{intent.getStringExtra("destination1"),
                                             intent.getStringExtra("destination2")};
        nextTimeGridHeader.setAdapter(new StringAdapter(getBaseContext(),destinations));

        spinner.setVisibility(View.VISIBLE);

        final TAG api = RetrofitClient.getClient().create(TAG.class);
        api.getHoraireByArret(intent.getStringExtra("parentStation")).enqueue(new Callback<List<ArretHoraire>>() {
            @Override
            public void onResponse(Call<List<ArretHoraire>> call, Response<List<ArretHoraire>> response) {
                List<Delay> delaysDest1 = new ArrayList<>();
                List<Delay> delaysDest2 = new ArrayList<>();
                for(ArretHoraire arretHoraire : response.body()){
                    if(arretHoraire.getPattern().getId().contains(intent.getStringExtra("routeId"))){ //SEM:B
                        if(arretHoraire.getPattern().getDir()==2){
                            delaysDest1.addAll(arretHoraire.getTimes());
                        }
                        if(arretHoraire.getPattern().getDir()==1){
                            delaysDest2.addAll(arretHoraire.getTimes());
                        }
                    }
                }
                Log.d("TAG on response", "" + delaysDest1.size());
                Log.d("TAG on response2", "" + delaysDest2.size());
                nextTimeGridContent1.setAdapter(new DelayAdapter(getBaseContext(),delaysDest1));
                nextTimeGridContent2.setAdapter(new DelayAdapter(getBaseContext(),delaysDest2 ));
                spinner.setVisibility(View.GONE);

            }

            @Override
            public void onFailure(Call<List<ArretHoraire>> call, Throwable t) {
                spinner.setVisibility(View.GONE);
            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return false;
    }

    @Override
    public void onBackPressed() {
        Intent i = new Intent();
        i.putExtra("ligne", getIntent().getStringExtra("shortName"));
        i.putExtra("routeId", getIntent().getStringExtra("routeId"));
        setResult(RESULT_OK, i);
        finish();
    }
}

Кроме того (не важно, но я хочу знать), имя этой активности - Arrets, когда я смотрю в AndroidStudioDesigner, я нашел «Ligne» в заголовке панели инструментов в activity_arret.xml (почему? Я не знаю ...), но «Arrets» в content_arret.xml. Где этот "Лайн"? Я искал в манифесте и в папке res, но не нашел. Это не важно, но я просто хочу знать, откуда взялась эта строка.


person Shining    schedule 01.05.2018    source источник


Ответы (2)


Потому что вы не можете добавить макет Fab в макет activity_arret.

добавьте строку ниже в макет activity_arret..

<include layout="@layout/fab_layout"/>  // here define your actionbar or fab layout.

ваш код ..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.grenoble.miage.miagemytag.ArretActivity"
tools:showIn="@layout/activity_arret"
android:layout_margin="16dp"
android:orientation="vertical">
<include layout="@layout/fab_layout"/>  // here define your actionbar or fab layout.
    <GridView
        android:id="@+id/nextTimeGridHeader"
        android:layout_width="match_parent"
        android:layout_height="36dp"
        android:layout_marginBottom="10dp"
        android:numColumns="2"
        android:stretchMode="columnWidth"
        android:clickable="false"/>
<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="0,1">
    <ProgressBar
        android:id="@+id/progressBar_arret"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <GridView
        android:id="@+id/nextTimeGridContent1"
        android:layout_width="170dp"
        android:layout_height="match_parent"
        android:clickable="false"
        android:numColumns="1"
        android:verticalSpacing="2dp"
        android:layout_column="0"/>
        <GridView
            android:id="@+id/nextTimeGridContent2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="1"
            android:verticalSpacing="2dp"
            android:clickable="false"
            android:layout_column="1"/>
    </TableRow>
</TableLayout>

person Android Team    schedule 01.05.2018

Попробуй это

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_message"
        app:elevation="3dp"
        app:layout_anchor="@+id/app_bar"
        app:layout_anchorGravity="right|bottom" />

    <include layout="@layout/custom_layout" />

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

ВЫВОД

введите здесь описание изображения

person AskNilesh    schedule 01.05.2018