私はAndroidを初めて使用し、この時点までAndroidの公式チュートリアルを正確に実行しました:https://developer.android.com/training/basics/actionbar/styling.htmlそして背景色を変更しようとしていますサポートライブラリ(Theme.AppCompactを使用)を使用して、アクションバーにデフォルトのAndroidアイコンを表示します。しかし、Android Studioでは、#AA0000のような16進数の色を設定しようとすると、(themes.xmlのサポートライブラリ行にある)「シンボルを解決できません」というエラーが表示されます。エラーがない場合は、Androidが表示されません。アクションバーの左側にあるアイコン。
期待は次のようなものです:
実際の結果(16進数を削除し、エラーを回避するために背景画像を設定した場合)は次のようになります。
左側のアイコンがありません(右側のメニュー下部もありませんが、問題が画面のインチ寸法に関連しているかどうかはわかりません)。
ただし、この状態では、背景画像のみを設定でき、16進色は設定できず、アイコンを表示できません。これらは私のファイルです(themes.xmlに16進色があります)。
これは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を使用している場合、アクションバーにアプリアイコンは表示されません。これは、そのクラスのデフォルトの動作です。ただし、見たい場合は、AppCompat-v721を使用してアクションバー/ツールバーにアイコンを表示してください。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加