简体   繁体   English

没有 appcompat 库的 showAsAction

[英]showAsAction without the appcompat library

I'm using a Toolbar (android.widget.Toolbar) set as ActionBar in my Activity (android.app.Activity) which has a menu item with an attribute android:showAsAction="always" .我在我的Activity (android.app.Activity) 中使用设置为 ActionBar 的Toolbar (android.widget.Toolbar),它有一个带有属性android:showAsAction="always"的菜单项。 So they are pure android classes without the appcompat (androidx.*) that I don't use app:showAsAction but android:showAsAction .所以它们是没有 appcompat (androidx.*) 的android 类,我不使用app:showAsAction而是android:showAsAction It seems that there is no problem.似乎没有问题。 But, Androidstudio warns like this screen shot:但是,Androidstudio 警告如下屏幕截图:

Androidstudio 警告 android:showAsAction

My question: Are there any possibilities that android:showAsAction usage causes any problem?我的问题: android:showAsAction使用是否有可能导致任何问题?

build.gradle (:app): build.gradle (:app):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "test.showasactiontest"
        minSdkVersion 22
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.google.android.material:material:1.1.0'
}

MainActivity.java:主活动.java:

package test.showasactiontest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toolbar;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setActionBar(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

res/layout/activity_main.xml res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

    <android.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme" />
</FrameLayout>

res/menu/main.xml:资源/菜单/main.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:icon="@android:drawable/ic_menu_view"
        android:showAsAction="always"
        android:title="Item" />
</menu>

I think correct answer of this question will be your response:我认为这个问题的正确答案将是你的回应:

Difference between android: and app: prefix in Android XML? android: 和 app: 前缀在 Android XML 中的区别?

Your warning is for using android:showAsAction="always" instead of app:showAsAction="always"您的警告是使用android:showAsAction="always"而不是app:showAsAction="always"

android is usually used for attribute coming from Android SDK itself. android通常用于来自 Android SDK 本身的属性。

app is often used if you are using the support library.如果您使用支持库,则经常使用app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM