gpt4 book ai didi

android - SDK21 上的操作栏图标

转载 作者:行者123 更新时间:2023-11-29 20:59:21 25 4
gpt4 key购买 nike

也许这是一个非常基本的问题,但我搜索了很多但没有找到解释为什么这不起作用...

我想做的是在操作栏左侧显示我的应用程序图标,不使用自定义 View ,也不将其启用为向上按钮...

我试过 setIcon、setLogo,都没有用...我记得前段时间显示图标是默认行为...

这是我的 build.gradle 和我的 MainActivity。

谢谢!

Build.Gradle

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.2'
}
}
apply plugin: 'com.android.application'

repositories {
jcenter()
}

android {
compileSdkVersion 21
buildToolsVersion "21.0.2"

defaultConfig {
applicationId "br.com.example.myapplication2.app"
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.0'
}

主 Activity

package br.com.example.myapplication2.app;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}

ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("My Profile");
actionBar.setIcon(R.drawable.ic_launcher);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}

最佳答案

根据 Toolbar documentation :

  • A title and subtitle. The title should be a signpost for the Toolbar's current position in the navigation hierarchy and the content contained there. The subtitle, if present should indicate any extended information about the current content. If an app uses a logo image it should strongly consider omitting a title and subtitle.

In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.

但是,如果您想要一个应用程序图标,setLogo()确实是正确的方法。

关于android - SDK21 上的操作栏图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26641974/

25 4 0