gpt4 book ai didi

android - 无法使用 Theme.AppCompat 设置十六进制颜色和查看操作栏上的图标

转载 作者:太空狗 更新时间:2023-10-29 13:18:06 24 4
gpt4 key购买 nike

我是 Android 的新手,到目前为止我一直完全遵循 Android 官方教程:https://developer.android.com/training/basics/actionbar/styling.html我正在尝试使用支持库(使用 Theme.AppCompact)更改背景颜色并查看操作栏上的默认 Android 图标。但是当我尝试设置像 #AA0000 这样的十六进制颜色时,Android Studio 给我错误“无法解析符号”(在 themes.xml 的支持库行上),而且如果没有错误,我也看不到 Android操作栏左侧的图标。

期望是这样的:

enter image description here

真正的结果(当我删除十六进制并设置背景图像以避免错误时)是这样的:

enter image description here

左边的图标不见了(右边的菜单底部也不见了,但我不知道问题是否与屏幕的英寸尺寸有关)。

但是,在这种情况下,我只能设置背景图像,不能设置十六进制颜色,而且我无法查看图标,这些是我的文件(themes.xml 中的十六进制颜色):

这是AndroidManifest.xml

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyActivity"
android:label="@string/app_name"
android:theme="@style/CustomActionBarTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName=".MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mycompany.myfirstapp.MyActivity" />
</activity>
</application>
<uses-sdk android:minSdkVersion="14" />

</manifest>

Themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyActionBar</item>

<!-- Support library compatibility -->
<item name="actionBarStyle">@style/MyActionBar</item>
</style>

<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#AA0000</item>

<!-- Support library compatibility -->
<item name="background">#AA0000</item>
</style>
</resources>

MyActivity.java

public class MyActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}

@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();

// Handle presses on the action bar items
switch (id) {
case R.id.action_search:
//openSearch();
Log.i("MyActivity", "SEARCH PRESSED");
return true;
case R.id.action_settings:
//openSettings();
Log.i("MyActivity", "SETTINGS PRESSED");
return true;
default:
return super.onOptionsItemSelected(item);
}
}

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}

activity_my.xml

<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"
android:orientation="horizontal" >

<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>

</LinearLayout>

**main_activity_actions.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>

</resources>

我该如何解决这个问题?

最佳答案

<style name="MyActionBar"
parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#AA0000</item>

<!-- Support library compatibility -->
<item name="background">#AA0000</item>
</style>
</resources>

这是行不通的,因为如果要使用它,您需要定义颜色并命名它。

在您的 res/values 文件夹中,应该有一个名为 colors.xml 的文件。如果没有,请在 res/values 文件夹中创建 colors.xml,然后将您的颜色定义添加到 colors.xml:

<item name="my_color" type="color">#AA0000</item>

然后通过资源名称引用它来使用颜色:

<style name="MyActionBar"
parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">@color/my_color</item>

<!-- Support library compatibility -->
<item name="background">@color/my_color</item>
</style>
</resources>

有关颜色的更多信息:https://developer.android.com/samples/BasicMediaRouter/res/values/colors.html

此外,在使用 AppCompatActivity 时,您不会在操作栏中看到应用程序图标。这只是该类的默认行为。但如果你想看它:show icon in actionbar/toolbar with AppCompat-v7 21

关于android - 无法使用 Theme.AppCompat 设置十六进制颜色和查看操作栏上的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32440712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com