我是 Android 的新手,到目前为止我一直完全遵循 Android 官方教程:https://developer.android.com/training/basics/actionbar/styling.html我正在尝试使用支持库(使用 Theme.AppCompact)更改背景颜色并查看操作栏上的默认 Android 图标。但是当我尝试设置像 #AA0000 这样的十六进制颜色时,Android Studio 给我错误“无法解析符号”(在 themes.xml 的支持库行上),而且如果没有错误,我也看不到 Android操作栏左侧的图标。
期望是这样的:
真正的结果(当我删除十六进制并设置背景图像以避免错误时)是这样的:
左边的图标不见了(右边的菜单底部也不见了,但我不知道问题是否与屏幕的英寸尺寸有关)。
但是,在这种情况下,我只能设置背景图像,不能设置十六进制颜色,而且我无法查看图标,这些是我的文件(themes.xml 中的十六进制颜色):
Đây làAndroidManifest.xml
package="com.mycompany.myfirstapp" >
<ứng>ứng>
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/tên_ứng_dụng"
android:theme="@style/AppTheme" >
android:name=".MyActivity"
android:label="@string/tên_ứng_dụng"
android:theme="@style/CustomActionBarTheme" >
android:name=".DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName=".MyActivity" >
<>
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mycompany.myfirstapp.MyActivity" />
Themes.xml
MyActivity.java
public class MyActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";
@Ghi đè
được bảo vệ void onCreate(Gói savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
@Ghi đè
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);
}
@Ghi đè
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");
trả về giá trị đúng;
case R.id.action_settings:
//openSettings();
Log.i("MyActivity", "SETTINGS PRESSED");
trả về giá trị đúng;
mặc định:
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
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="phù hợp với cha mẹ"
android:layout_height="match_parent"
android:orientation="horizontal" >
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>
**main_activity_actions.xml
xmlns:app="http://schemas.android.com/apk/res-auto">
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="ifRoom" />
android:title="@string/action_settings"
app:showAsAction="never" />
styles.xml