如何在 Android 应用程序中使用 AwesomeBar 库?
AwesomeBar是一个动画库,可以非常轻松地在应用栏中集成各种特性和功能,例如导航抽屉、操作按钮和溢出菜单。在本文中,我们将使用Java语言在 Android 应用程序中实现这个库。下面给出了一个示例 GIF,以了解我们将在本文中做什么。
分步实施
第 1 步:创建一个新项目
要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Java作为编程语言。
第二步:添加库依赖
导航到Gradle Scripts > build.gradle(Module:app) ,在依赖项部分添加库,并同步项目。
dependencies {
implementation 'com.github.florent37:awesomebar:1.0.3'
}
步骤 3:使用 activity_main.xml 文件
导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码。
XML
Java
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;
import com.github.florent37.awesomebar.ActionItem;
import com.github.florent37.awesomebar.AwesomeBar;
public class MainActivity extends AppCompatActivity {
AwesomeBar bar;
DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = findViewById(R.id.drawer_layout);
bar = findViewById(R.id.bar);
// adding action button in AwesomeBar with drawable and text
bar.addAction(R.drawable.awsb_ic_edit_animated, "Add Post");
// adding the OverFlowItem item in AwesomeBar
bar.addOverflowItem("Settings");
bar.addOverflowItem("About");
bar.setActionItemClickListener(new AwesomeBar.ActionItemClickListener() {
@Override
public void onActionItemClicked(int position, ActionItem actionItem) {
// toast is shown when action item is pressed.
Toast.makeText(getBaseContext(), actionItem.getText() + " clicked",Toast.LENGTH_SHORT).show();
}
});
bar.setOverflowActionItemClickListener(new AwesomeBar.OverflowActionItemClickListener() {
@Override
public void onOverflowActionItemClicked(int position, String item) {
// toast is shown when an OverFlowAction item is pressed.
Toast.makeText(getBaseContext(), item + " clicked", Toast.LENGTH_SHORT).show();
}
});
bar.setOnMenuClickedListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// opens the navigation drawer from start
Toast.makeText(getBaseContext(), "Opening Navigation Drawer",Toast.LENGTH_SHORT).show();
drawerLayout.openDrawer(Gravity.START);
}
});
// setting true changes the clickable icon as "<"
bar.displayHomeAsUpEnabled(false);
}
}
第 4 步:使用 MainActivity。 Java文件
转到主活动。 Java文件,参考如下代码。下面是MainActivity的代码。 Java文件。代码中添加了注释以更详细地理解代码。
Java
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;
import com.github.florent37.awesomebar.ActionItem;
import com.github.florent37.awesomebar.AwesomeBar;
public class MainActivity extends AppCompatActivity {
AwesomeBar bar;
DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = findViewById(R.id.drawer_layout);
bar = findViewById(R.id.bar);
// adding action button in AwesomeBar with drawable and text
bar.addAction(R.drawable.awsb_ic_edit_animated, "Add Post");
// adding the OverFlowItem item in AwesomeBar
bar.addOverflowItem("Settings");
bar.addOverflowItem("About");
bar.setActionItemClickListener(new AwesomeBar.ActionItemClickListener() {
@Override
public void onActionItemClicked(int position, ActionItem actionItem) {
// toast is shown when action item is pressed.
Toast.makeText(getBaseContext(), actionItem.getText() + " clicked",Toast.LENGTH_SHORT).show();
}
});
bar.setOverflowActionItemClickListener(new AwesomeBar.OverflowActionItemClickListener() {
@Override
public void onOverflowActionItemClicked(int position, String item) {
// toast is shown when an OverFlowAction item is pressed.
Toast.makeText(getBaseContext(), item + " clicked", Toast.LENGTH_SHORT).show();
}
});
bar.setOnMenuClickedListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// opens the navigation drawer from start
Toast.makeText(getBaseContext(), "Opening Navigation Drawer",Toast.LENGTH_SHORT).show();
drawerLayout.openDrawer(Gravity.START);
}
});
// setting true changes the clickable icon as "<"
bar.displayHomeAsUpEnabled(false);
}
}
输出:
GitHub 存储库: AwesomeBar 库