我不知道为什么,它之前有效但现在我的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>
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时,我找到了&#34; Ligne&#34;在activity_arret.xml的工具栏标题中(为什么?我不知道......)但是&#34; Arrets&#34;在content_arret.xml中。这是什么&#34; Ligne&#34; ?我在清单和res文件夹中搜索但我找不到。这并不重要,但我只是想知道这个字符串的来源。
答案 0 :(得分:0)
因为您无法将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>
答案 1 :(得分:0)
试试这个
<?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>
<强>输出强>