我是编程新手,我想使用导航抽屉FloatingActionButton
。我可以单独制作这两个,但我不能在一个项目中完成。
我真的不想要第三方图书馆。
编辑:
我想使用导航抽屉和FloatingActionButton
来打开不同的片段。
默认情况下,这是导航抽屉,我需要向FloatingActionB
MainActivity.Java
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
app_bar_main.xml(有浮动按钮)
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="160dp"
android:layout_gravity="bottom|end"
android:layout_marginRight="@dimen/fab_margin"
android:visibility="invisible"
app:backgroundTint="@color/colorPrimaryDark"
app:elevation="6dp"
app:pressedTranslationZ="12dp"
app:srcCompat="@android:drawable/btn_radio" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="90dp"
android:layout_gravity="bottom|end"
android:layout_marginRight="@dimen/fab_margin"
android:visibility="invisible"
app:elevation="6dp"
app:backgroundTint="@color/colorPrimary"
app:pressedTranslationZ="12dp"
app:srcCompat="@android:drawable/ic_menu_manage" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:elevation="6dp"
app:backgroundTint="@color/colorAccent"
app:pressedTranslationZ="12dp"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_menu_add" />
答案 0 :(得分:0)
试试这个
将其添加到现有代码中:
在onCreate()之外的类中添加此代码。
@Override
public void onDrawerSlide(View drawerView, float offset){
fabButton.setAlpha(offset);
}
如果您不想切换到不同的FAB实施。使用onDrawerSlide(View drawerView, float offset)
并在打开抽屉时更改FAB alpha。您也可以在onDrawerClose() or onDrawerOpen()
方法中切换其可见性。
fab按钮编程就是这个。这是设置图像和代码的代码。 FAB的位置和其他属性。
fabButton = new FloatingActionButton.Builder(this)
.withDrawable(getResources().getDrawable(R.drawable.ic_action_edit))
.withButtonColor(0xFF2196F3)
.withGravity(Gravity.BOTTOM | Gravity.END)
.withMargins(0, 0, 16, 16)
.create();
试着希望它可以帮助你。
答案 1 :(得分:0)
Here's您应用中的材料设计实施指南。
尽管如此,我建议您制作新的Navigation drawer
活动。转到
File>New>Activity>Navigation Drawer Activity.
此activity
已经附带了FAB和Navigation Drawer
,并且已经覆盖了onOptionsItemSelected
(),onNavigationItemSelected
()等方法。所以你需要的只是将您的布局放在content_main
文件中。
答案 2 :(得分:0)
尝试在DrawerLayout中包含协调器布局:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mDrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent
android:layout_width="wrap_content"
android:id="@+id/mNavigationView"
android:layout_gravity="start"
app:menu="@menu/navigation_menu"/>
</android.support.v4.widget.DrawerLayout>
然后在单独实施
时在协调器布局中执行任何操作答案 3 :(得分:0)
我使用此代码显示来自FAB浮动操作按钮的导航
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.menu_button);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
}
});
如果您想使用Kotlin,则类似于以下代码
var drawer = findViewById<DrawerLayout>(R.id.drawer_layout)
val fab = findViewById<FloatingActionButton>(R.id.menu_button)
fab.setOnClickListener {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
}
这是我的布局,您可以使用上面的Java或Kotlin代码之一,并且对我有效。
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
tools:openDrawer="start">
<include
layout="@layout/nav_host_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_nav_view"/>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu_left" />
</androidx.drawerlayout.widget.DrawerLayout>