Android Q:应用内设置面板
时间:2020-02-23 14:29:10 来源:igfitidea点击:
在本教程中,我们将讨论如何在Android应用程序中显示"设置面板"。
随着Android Q的推出,无需切换到"设置"应用即可显示浮动设置菜单。
Android Q设置面板
设置面板就像"底部浮动菜单",它使我们可以查看/编辑设置。
当前,"设置面板"可以显示三种不同类型的设置:
连接设置–移动数据/Wifi飞机模式
音量设置–更改警报,呼叫通知等的音量。
NFC –配置NFC连接设置
要调用每个这样的设置,我们需要使用特定的操作字符串常量来调用Intent。
以下是每个面板的操作字符串:
- ACTION_INTERNET_CONNECTIVITY
- ACTION_NFC
- ACTION_VOLUME
使用上面的"设置面板"可使UX更加流畅。
要从当前应用程序更改设置,用户必须使用Intents启动设置应用程序。
现在,过渡更加顺畅。
尽管有很多优点,但这种新的"设置面板"设计还是有缺点的。
需要考虑的事实:当用户可以从状态列中的系统通知托盘访问这些操作时,为什么真正需要设置面板?
在下一节中,我们将在Android Studio项目中实现这三个设置面板中的每一个。
Android Q应用内设置面板项目结构
Android Q设置面板项目结构
请确保您已更新Android SDK,并已构建到最新的Android –Q。
代码
下面给出了activity_main.xml布局的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/btnInternet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="INTERNET SETTINGS"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
<Button
android:id="@+id/btnVolume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="VOLUME SETTINGS"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
<Button
android:id="@+id/btnNFC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NFC SETTINGS"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
</LinearLayout>
MainActivity.java类的代码如下:
package com.theitroad.androidqsettingspanel;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btnInternet, btnVolume, btnNFC;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnInternet = findViewById(R.id.btnInternet);
btnVolume = findViewById(R.id.btnVolume);
btnNFC = findViewById(R.id.btnNFC);
btnInternet.setOnClickListener(this);
btnVolume.setOnClickListener(this);
btnNFC.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnInternet:
showInternetSettings();
break;
case R.id.btnVolume:
showVolumeSettings();
break;
case R.id.btnNFC:
showNFCSettings();
break;
}
}
private void showInternetSettings() {
startActivity(new Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY));
}
private void showVolumeSettings() {
startActivity(new Intent(Settings.Panel.ACTION_VOLUME));
}
private void showNFCSettings() {
startActivity(new Intent(Settings.Panel.ACTION_NFC));
}
}
上面的代码是不言自明的。
我们只是在不同的按钮单击意图上启动了每个设置面板。
上面应用程序的输出如下:
Android Q设置面板输出
由于Android模拟器存在问题,因此请忽略滞后和UI减少的问题。

