我试图创建一个带有滑动标签的循环浮动操作菜单按钮。主浮动操作按钮正在运行,但子菜单按钮的单击事件不起作用。
以下是菜单按钮的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_menu_compass"
android:visibility="invisible"
app:backgroundTint="@color/colorAccent"
app:fabSize="mini" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_menu_myplaces"
android:visibility="invisible"
app:backgroundTint="@color/colorAccent"
app:fabSize="mini" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_menu_share"
android:visibility="invisible"
app:backgroundTint="@color/colorAccent"
app:fabSize="mini" />
</FrameLayout>
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<android.support.v4.widget.DrawerLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout">
<LinearLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/white"
>
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tab"
style="@style/MyCustomTabLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:tabMode="scrollable"
/>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinatorLayout">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
<include layout="@layout/fab_layout" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
<fragment
android:id="@+id/nav_drawer"
android:name="com.example.usaukglu.mywallet.NavigationDrawer"
android:layout_width="@dimen/drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/navigation_drawer"
tools:layout="@layout/navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
这是MainActivity.java类:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
NavigationDrawer navigationDrawer;
DrawerLayout drawerLayout;
FloatingActionButton fab;
FloatingActionButton fab1;
FloatingActionButton fab2;
FloatingActionButton fab3;
CoordinatorLayout coordinatorLayout;
private boolean FAB_status=false;
Animation show_fab_1;
Animation hide_fab_1;
Animation show_fab_2;
Animation hide_fab_2;
Animation show_fab_3;
Animation hide_fab_3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tab);
toolbar = (Toolbar) findViewById(R.id.app_bar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), MainActivity.this));
tabLayout.setupWithViewPager(viewPager);
navigationDrawer = (NavigationDrawer) getSupportFragmentManager().findFragmentById(R.id.nav_drawer);
navigationDrawer.setUp(R.id.nav_drawer, drawerLayout, toolbar);
coordinatorLayout= (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
fab= (FloatingActionButton) findViewById(R.id.fab);
fab1= (FloatingActionButton) findViewById(R.id.fab_1);
fab2= (FloatingActionButton) findViewById(R.id.fab_2);
fab3= (FloatingActionButton) findViewById(R.id.fab_3);
show_fab_1= AnimationUtils.loadAnimation(getApplication(),R.anim.fab1_show);
hide_fab_1= AnimationUtils.loadAnimation(getApplication(),R.anim.fab1_hide);
show_fab_2= AnimationUtils.loadAnimation(getApplication(),R.anim.fab2_show);
hide_fab_2= AnimationUtils.loadAnimation(getApplication(),R.anim.fab2_hide);
show_fab_3= AnimationUtils.loadAnimation(getApplication(),R.anim.fab3_show);
hide_fab_3= AnimationUtils.loadAnimation(getApplication(),R.anim.fab3_hide);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (FAB_status == false) {
//Display FAB menu
expandFab();
FAB_status = true;
} else {
//Close FAB menu
hideFAB();
FAB_status = false;
}
}
});
fab1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplication(), "Floating Action Button 1", Toast.LENGTH_SHORT).show();
}
});
fab2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplication(), "Floating Action Button 2", Toast.LENGTH_SHORT).show();
}
});
fab3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplication(), "Floating Action Button 3", Toast.LENGTH_SHORT).show();
}
});
}
答案 0 :(得分:-1)
添加到XML布局:
<android.support.design.widget.FloatingActionButton
android:id="@+id/myFAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_icon"
app:elevation="4dp"
... />
添加到后面的代码:
FloatingActionButton myFab = (FloatingActionButton) myView.findViewById(R.id.myFAB);
myFab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO something when floating action menu third item clicked
}
});
有关详细信息,请点击链接
FloatingActionButton example with Support Library
http://www.viralandroid.com/2016/02/android-floating-action-menu-example.html