gpt4 book ai didi

android - float 操作按钮不会停留在 SnackBar 之上

转载 作者:行者123 更新时间:2023-11-30 01:58:48 25 4
gpt4 key购买 nike

我已经处理这个问题好几天了。我根本无法让 FloatingActionButton 位于 SnackBar 之上。

我看过很多教程..什么都试过了。

我希望有人能在这里帮助我:

我的布局是:

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
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.CoordinatorLayout
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

<!-- Your Scrollable View -->

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:src="@drawable/ic_start"
app:fabSize="normal"/>

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

我的 Activity 代码是:

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity implements OnClickListener {

DrawerLayout drawerLayout;
Toolbar toolbar;
FloatingActionButton fab;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

setupToolbar();
setupFab();
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if(drawerLayout != null)
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}

private void setupNavigationView(){

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
}

private void setupFab(){
fab = (FloatingActionButton) findViewById(R.id.fab);
if(fab != null)
fab.setOnClickListener(this);
}
private void setupToolbar(){
toolbar = (Toolbar) findViewById(R.id.toolbar);
if(toolbar != null)
setSupportActionBar(toolbar);

// Show menu icon
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
}

@Override
public void onClick(View view) {

if(view.getId() == R.id.fab){

Snackbar
.make(findViewById(R.id.coordinatorLayout), "This is Snackbar", Snackbar.LENGTH_LONG)
.setAction("Action", this)
.show(); // Don�t forget to show!
}
}
}

结果是这样的:

result

最佳答案

而不是使用 coordinatorLayout,但实际上你需要 FloatingActionButton 的 View :

@Override
public void onClick(View view) {
if(view.getId() == R.id.fab) {
Snackbar .make(view, "This is Snackbar", Snackbar.LENGTH_LONG) .setAction("Action", this) .show();
}
}

关于android - float 操作按钮不会停留在 SnackBar 之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31807825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com