如何在 Android 中移除 Action Bar 下方的阴影?
默认情况下,Android 为 Action Bar 提供阴影。本例演示了如何移除 Action Bar 下方的阴影。
步骤 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" android:id = "@+id/parent" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" android:gravity = "center" android:orientation = "vertical"> <TextView android:id = "@+id/text" android:textSize = "18sp" android:textAlignment = "center" android:text = "remove shadow for action bar" android:layout_width = "match_parent" android:layout_height = "wrap_content" /> </LinearLayout>
步骤 3 - 将以下代码添加到 src/MainActivity.java 中。
package com.example.andy.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int view = R.layout.activity_main;
TextView text;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(view);
getSupportActionBar().setElevation(0);
text = findViewById(R.id.text);
}
}在上面的代码中,我们使用了 setElevation(0) 来禁用 Action Bar 的阴影。完整代码如下所示 -
getSupportActionBar().setElevation(0);
让我们尝试运行您的应用程序。我假设您已将您的实际 Android 手机设备连接到您的计算机。要从 Android Studio 运行应用程序,请打开项目的某个 Activity 文件,然后单击工具栏中的运行
图标。选择您的手机设备作为选项,然后检查您的手机设备,它将显示您的默认屏幕 -

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