如何在Android中创建自定义操作栏?
在进入示例之前,我们应该了解Android中的操作栏是什么。操作栏就像Android中的页眉。我们可以对所有屏幕使用相同的操作栏,也可以为特定活动更改操作栏。
此示例演示了如何在Android中创建自定义操作栏。
步骤1 - 在Android Studio中创建一个新项目,转到文件⇒新建项目,并填写创建新项目所需的所有详细信息。
步骤2 - 将以下代码添加到res/layout/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" tools:context = ".MainActivity"> <TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Custom Action Bar" android:textSize = "20sp"/> </LinearLayout>
步骤2 - 将以下代码添加到src/MainActivity.java中
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.custom_action_bar);
//getSupportActionBar().setElevation(0);
View view = getSupportActionBar().getCustomView();
TextView name = view.findViewById(R.id.name);
name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "You have clicked tittle", Toast.LENGTH_LONG).show();
}
});
}
}步骤3 - 在res文件夹中为操作栏创建一个布局,作为custom_action_bar.xml,如下所示
<?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" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:gravity = "center_vertical" android:padding = "10dp" android:weightSum = "1"> <LinearLayout android:layout_width = "0dp" android:layout_height = "match_parent" android:layout_weight = "0.6"> <ImageView android:layout_width = "wrap_content" android:layout_height = "match_parent" android:src = "@drawable/ic_face_red_400_24dp" /> <TextView android:id = "@+id/name" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginLeft = "10dp" android:text = "Instagram" android:textSize = "20sp" android:textColor = "#000" android:textStyle = "bold" app:fontFamily = "@font/allan_bold" /> </LinearLayout> <LinearLayout android:layout_width = "0dp" android:layout_height = "match_parent" android:layout_marginRight = "10dp" android:layout_weight = "0.4" android:gravity = "end"> <ImageView android:layout_width = "wrap_content" android:layout_height = "match_parent" android:src = "@drawable/ic_local_post_office_red_400_24dp" /> <ImageView android:layout_width = "wrap_content" android:layout_height = "match_parent" android:layout_marginLeft = "20dp" android:src = "@drawable/ic_send_red_400_24dp" /> </LinearLayout> </LinearLayout>
注意 - 根据项目/应用程序规范,我们需要更改自定义布局。

让我们尝试运行您的应用程序。我假设您已将您的实际Android移动设备连接到您的计算机。要从Android Studio运行应用程序,请打开项目的活动文件之一,然后单击运行
工具栏中的图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕。
要删除操作栏按钮阴影,请在MainActivity中的onCreate()中使用以下代码,如下所示
this.getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); getSupportActionBar().setDisplayShowCustomEnabled(true); getSupportActionBar().setCustomView(R.layout.custom_action_bar); getSupportActionBar().setElevation(0); View view = getSupportActionBar().getCustomView();
现在运行您的应用程序,它将给出如下所示的输出 -

点击此处下载项目代码
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP