【问题标题】:FAB not visible with CoordinatorLayout使用 CoordinatorLayout 看不到 FAB
【发布时间】:2018-05-01 11:44:56
【问题描述】:

我不知道为什么,它以前可以工作,但现在我的 FAB 在我的应用程序中不可见 我使用了一个图库示例来创建此活动 (scrollingActivity),但我已将 NestedScrollView 更改为 LinearLayout。不知道是不是这个原因。

现在我只想让我的 FAB 出现在我的视野中,但我不知道该怎么做。

在设计器中查看: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>

JAVA

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 时,我在 activity_arret.xml 的工具栏标题中找到了“Ligne”(为什么?我不知道......)但是“在 content_arret.xml 中的 Arrets。这个“Ligne”在哪里?我在清单和 res 文件夹中进行了搜索,但没有找到。这并不重要,但我只想知道这个字符串是从哪里来的。

【问题讨论】:

    标签: android android-layout android-nestedscrollview floating-action-button


    【解决方案1】:

    因为你不能将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>
    

    【讨论】:

      【解决方案2】:

      试试这个

      <?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>
      

      输出

      【讨论】:

        猜你喜欢
        • 2019-09-29
        • 2017-12-02
        • 2019-12-20
        • 2016-11-14
        • 2015-12-21
        • 1970-01-01
        • 1970-01-01
        • 2016-11-14
        • 2015-12-22
        相关资源
        最近更新 更多