Androidアプリ開発でアクションバー背景色の変更方法を教えてください

このQ&Aのポイント
  • Androidのアクションバー及びタブの背景色を変更したいです。
  • AndroidStudioで開発を行っており、style.xmlを書き換えたりしています。
  • しかし、変更が反映されずに困っています。解決方法を教えてください。
回答を見る
  • ベストアンサー

Androidのアクションバー背景色を変更したい

独学でAndroidアプリ開発を勉強しています。 AndroidStudioで開発を行っており、タブの背景色を変更したいと思い、 いろいろなサイトを参考にstyle.xmlを書き換えたりしているのですが、 一向に変更されず、行き詰まっています。 【res/values/style.xml】 <resources> <!-- Base application theme. --> <style name="AppTheme" parent="@style/Theme.AppCompat"> <item name="android:actionBarStyle">@style/MyActionBar</item> <!-- Customize your theme here. --> </style> <style name="MyActionBar" parent="Widget.AppCompat.Light.ActionBar"> <item name="android:background">@color/background_color</item> </style> </resources> 【res/values/color.xml】 <?xml version="1.0" encoding="utf-8"?> <resources> <color name="background_color">#3ADBC6</color> </resources> 【AndroidManifest.xml】 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="jp.hogehoge.tabtestapp" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 最終的には、アクションバー及びタブの背景色を変更したいのですが、 どこか間違っている部分があるでしょうか? どなたか、ご指導いただけるとありがたいです。 どうぞ宜しくお願い致します。

質問者が選んだベストアンサー

  • ベストアンサー
  • HNEX
  • ベストアンサー率62% (43/69)
回答No.1

AppCompatを使用しているので、Styleもバージョンでわけないといけないですね。 ActionBarはたしか3.0で追加されたものなので、それ以前のバージョンでは使えず、それを使えるようにするのがAppCompatな訳ですが、これを利用するにしても結局バージョンを意識してかかないといけないというなんとも面倒なものなのです。 この煩わしい問題を解決してくれる素晴らしいツールがあるので、こちらを試してみては如何でしょうか? このページでデザインしたものをダウンロードすればstyle.xmlも含まれているので、それを見るとどのようにしているのかも見ることが出来ます。

参考URL:
http://jgilfelt.github.io/android-actionbarstylegenerator/

関連するQ&A

  • Androidアプリ・SDKでの実行時エラー

    初めてアンドロイドアプリの開発をしています(多言語のプログラミング経験はありますが、JAVAは初めてです)。Eclipseで開発を行っていますが、ボタンをクリックしたら画面遷移をする、という処理を追加したところ、Android SDKで実行した際に 「アプリ名(パッケージ名)が予期せず停止しました。やり直してください。」 と出て、トップの画面も表示されなくなってしまいました。なお、Eclipse上ではエラーなどが出ていません。原因が解らず困っています。何かわかる方がいらっしゃいましたらお教え下さい。 以下がソースになります。 ■トップのActivity--------------------- package jp.xxxxx.xxxxx; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //ボタンを定義 Button btn1 = (Button) findViewById(R.id.btn1); //クリックイベント btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent intent = new Intent( getApplicationContext() , SubActivity.class); startActivity(intent); } }); } } ■トップ画面のXML--------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"> <ImageView android:id="@+id/topLogo" android:src="@drawable/logo" /> <LinearLayout android:id="@+id/linearLayout1"> <TextView android:id="@+id/btn1" android:text="@string/btn1text"></TextView> </LinearLayout> </LinearLayout> ■トップのAndroidManifest.xml--------------------- <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="jp.xxxxx.xxxxx" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:label="@string/app_name" android:name="MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:label="@string/screenName2" android:name="SubActivity"> </activity> </application> </manifest> なお、Activityの「//ボタンを定義」以下をコメントアウトした際には画面は出てきます。何か初歩的な間違いをしているかもしれませんが、どうかよろしくお願いします。

    • ベストアンサー
    • Java
  • Androidの画面遷移ができません(その2)

    前回の質問の続きです. 今度はページXXXXXから3つのボタン(AAAAA,BBBBB,CCCCCと仮定)を画面上に表示させ,ボタンに記載された通りの各ページAAAAA,BBBBB,CCCCCに画面遷移しようと考えています. AAAAA,BBBBB,CCCCCにはそれぞれ異なった画像が表示されるようにプログラミングしてあるのですが,すべてのボタンを押してもページAAAAAに画面遷移してしまい困っています. おそらくAndroidManifest.xmlの定義が間違えているのではないかと思っているのですが,どこに原因があるのでしょうか?解答よろしくお願いします. 【AndroidManifest.xml】 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="sample.XXXXX" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="9" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".XXXXX" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="AAAAA"></activity> <activity android:name="BBBBB"></activity> <activity android:name="CCCCC"></activity> </application> </manifest>

  • Android Icon のイベント

    ActionBarActivity が非推奨となり AppCompatActivity を継承して Activity を作ってみたのですが、Toolbar の Icon のイベントの取得方法が分かりません。 (ここで「Icon又はLogoの表示」を ご教授いただいたばかりなのですが) Toolbar の Icon のイベントの取得は そんなに難しくないだろうと思って、イロイロ検索しても全然ダメでした。 Toolbar の Icon のイベントの取得方法を ご存知の方が居ましたら ご教授下さいませm(_ _)m ◆ ThemeでActionBarを使用しない設定 『AndroidManifest.xml』内 <application android:theme="@style/AppTheme" ~ 『values/styles.xml』内 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> ◆「android.support.v7.widget.Toolbar」をsetSupportActionBarに設定 『layout/activity_main.xml』内 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" ~ <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" android:background="#9b7fff" app:theme="@style/ThemeOverlay.AppCompat.ActionBar" /> ◆ Iconを表示 『MainActivity.java』内 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ActionBar oActionBar = getSupportActionBar( ); // oActionBar.setLogo(R.drawable.~); // Toolbar に LogoIcon を表示。 oActionBar.setIcon(R.drawable.~);// Toolbar に Icon を表示。

  • Java初心者なので教えてください

    上記のサイトをコピペしたんですけどできません。 対策を教えてください。 ↓参考URL http://labs.agenda-style.jp/blog/2011/02/android---google--.html <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android.googlemaps" android:versionCode="1" android:versionName="1.0"> <uses-permission android:name="android.permission.INTERNET"/> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-library android:name="com.google.android.maps"/> <activity android:name=".GoogleMapsActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> このプログラムにエラが出ています

  • [Android]エミュレータで動作しない原因

    こんにちは。java、Android開発初心者です。 「test001」というAndroidプロジェクト、「test001_01」というクラスを作成し、マニフェストを書き換えました。実行すると、再起動して繰り返しても The application has stopped unexpectedly. Please try again. と表示されます。 原因と対処方法を教えていただけませんか。 宜しくお願い致します。 ★test001 Manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test001" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.test001.text001_01" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> ★test001_01.java package com.example.test001; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.view.Menu; import android.widget.Button; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.TextView; public class test001_01 extends Activity { private Button startButton, stopButton; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sub); startButton = new Button(this); startButton.setText("Button5"); stopButton = new Button(this); stopButton.setText("Button6"); textView = new TextView(this); textView.setText("TextView1"); textView.setBackgroundColor(Color.YELLOW); LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); LinearLayout linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); linearLayout.setLayoutParams(linearLayoutParams); linearLayout.addView(startButton, linearLayoutParams); linearLayout.addView(stopButton, linearLayoutParams); linearLayout.addView(textView, linearLayoutParams); setContentView(linearLayout); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }

    • ベストアンサー
    • Java
  • Androidコンパイル時のエラーについて

    Androidの開発中に下記エラーが出ます。 対処の解る方がおりましたらば教えていただけますでしょう 宜しくお願いいたします。 詳細内容及びソース、XML エラー 2011-05-11 16:09:27 - BMICalculator2] res\layout\main.out.xml:0: Originally defined here. [2011-05-11 16:09:27 - BMICalculator2] C:\workspace\BMICalculator2\res\values\strings.out.xml:1: エラー: Error parsing XML: no element found [2011-05-11 16:09:27 - BMICalculator2] C:\workspace\BMICalculator2\res\layout\main.out.xml:1: エラー: Error parsing XML: no element found [2011-05-11 16:09:27 - BMICalculator2] C:\workspace\BMICalculator2\res\layout\main.xml:38: エラー: エラー: No resource found that matches the given name (at 'id' with value '@id+/button_calculate'). [2011-05-11 16:09:28 - BMICalculator2] res\layout\main.xml:0: エラー: Resource entry main is already defined. [2011-05-11 16:09:28 - BMICalculator2] res\layout\main.out.xml:0: Originally defined here. [2011-05-11 16:09:28 - BMICalculator2] C:\workspace\BMICalculator2\res\values\strings.out.xml:1: エラー: Error parsing XML: no element found [2011-05-11 16:09:28 - BMICalculator2] C:\workspace\BMICalculator2\res\layout\main.out.xml:1: エラー: Error parsing XML: no element found [2011-05-11 16:09:28 - BMICalculator2] C:\workspace\BMICalculator2\res\layout\main.xml:38: エラー: エラー: No resource found that matches the given name (at 'id' with value '@id+/button_calculate'). ソース package com.mamezou.android.bmi.alc; import android.app.Activity; import android.os.Bundle; public class BMICalculatorActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } string.XML <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello.Wopld.BMICalculatorActivity</string> <string name="app_name">BMI Calculator</string> <string name="label_description">BMIを計算します</string> <string name="label_height">身長(cm)</string> <string name="label_weight">体重(kg)</string> <string name="button_calculate">計算</string> </resources> main.XML <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/label_description" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/label_height" /> <EditText android:id="@+id/text_height" android:layout_width="fill_parent" android:layout_height="wrap_content" android:numeric="integer" android:maxLength="3" android:text="160" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/label_weight" /> <EditText android:id="@+id/text_weight" android:layout_width="fill_parent" android:layout_height="wrap_content" android:numeric="integer" android:maxLength="3" android:text="50" /> <Button android:id="@+id/button_calculate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_calculate" /> </LinearLayout>

  • Google maps API v2使用アプリ

    を作っています。 ただマップを表示させるだけで言いのですが、色んなサイトを参考にして作っても毎回問題が発生したため終了しますと出て、起動できません。 試しにgoogle-play-servicesのサンプルのmapsをインストールしたのですが、同じでした。 (もちろんAPIキーは取得して、書き換えてます) インストールはeclipseでデバッグなどしてできたapkをSDカード上に置き、それをスマホ側でタッチしてインストールしてます。 サンプルすら動かないとなると、自分のスマホに問題ありですかね? 二年くらい前のものですが、一応Android4.0.4なのですが… 何が問題なのか分かりません。もちろんeclipse上のコードにエラーはでてないです。 下に長いですが、メイン、マニフェスト、レイアウトを載せます。 他に必要な情報があれば教えてください。 メイン文は package com.example.gps2; import android.os.Bundle; import android.support.v4.app.FragmentActivity; public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps);//レイアウトを読み込む } } レイアウトは <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.SupportMapFragment" /> マニフェストは <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.gps2" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="18" android:targetSdkVersion="18" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-feature android:glEsVersion="0x00020000" android:required="true"/> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyAil_zFbdUol0hpUmtqHvMO84jj7Pl-658"/> <permission android:name="com.example.gps2.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> <uses-permission android:name="com.example.gps2.permission.MAPS_RECEIVE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> となってます。

  • androidアプリ開発について

    androidアプリ開発初心者です。 やりたいことは、 スタートボタンを押して、自分のお気に入りのページに飛び、(例えばオークションなど)サイトを表示させ、何分間ごとに更新できるループ処理?などができればいいなと考えています。 スタートボタンを押してサイトを表示するまでは何とか作れたのですが、それ以降をどのように作ったらいいのか分かりません。 初心者的な質問で申し訳ないのですが、どのように書いたらよいのか教えてください。 コードはこのようになっています。 <<main.xml>> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="192dp" android:layout_height="wrap_content" android:text="スタート" /> </LinearLayout> <<MainActivity.java>> import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button btn =null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ Uri uri =Uri.parse("http://"); Intent intent=new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(uri); startActivity(intent); } }); } @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; } } 長文で申し訳ありません。どうぞ宜しくお願い致します。

  • プルダウン 背景色の変更

    お世話になっております。 以前こちらで質問させていただき、またつまずいたので質問させてください。 何度も申し訳ございません。 現在、 <script type="text/javascript"> <!-- var bc = ["#FF0000","#00FF00","#0000FF","#FFFFFF"]; function chBackGround(e) { e.style.backgroundColor=bc[e.selectedIndex]; } window.onload = function() { document.getElementById('key').style.backgroundColor=bc[0]; } // --> </script> ------------------------------------------------------------ html部分 <select id="key" name="key" onChange="chBackGround(this)"> <option style="background-color: #FF0000;">サンプル0</option> <option style="background-color: #00FF00;">サンプル1</option> <option style="background-color: #0000FF;">サンプル2</option> <option style="background-color: #FFFFFF;">サンプル3</option> </select> としており、プルダウンで選ばれたカラーのスタイルを<select>タグに埋め込んでおります。 上記だと、この画面を初期表示時、<select>部分に読み込まれるプルダウンの色が、必ずbc[0](サンプル0の色)になります。 例えば、サンプル2のoptionにselectedが記載されていた場合、画面の初期表示はbc[2](サンプル2の色)にしたいのですが、この方法をご教授いただけませんでしょうか。 お忙しいところ申し訳ございません。 どうぞ宜しくお願いいたします。

  • Android Studio Kotlin エラー

    全部のファイルのR.layout.hogehoge_activityのところのRで赤くなってエラーが出ます。 API28 revision 6 emulator-5554 Android 9 Android Studio 3.1.4 SDK tools 26.1.1 言語 Kotlin たくさんアクティビティファイルがありますので、エラー内容がわかるであろう、MainActivityとbuild.gradleと、Manifestファイル、を書きました。 インターネットで調べたのですが、有益な情報が見つかりませんでしたので、詳しい方、お願い済ます。 エラー内容を記します。 FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:processDebugResources'. > Failed to process resources, see aapt output above for details. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 0s 13 actionable tasks: 1 executed, 12 up-to-date 1つめのファイル MainActivity package com.example.yusuke.mysql02 import android.content.Context import android.content.Intent import android.content.SharedPreferences import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.preference.PreferenceManager import android.view.View import android.widget.EditText import com.example.yusuke.mysql02.Grobal.grobalusername import com.example.yusuke.mysql02.R.id.username import android.widget.TextView import android.widget.Toast import com.android.volley.Request import com.android.volley.toolbox.StringRequest import kotlinx.android.synthetic.main.activity_main.* import com.android.volley.Response import com.android.volley.toolbox.JsonObjectRequest import com.android.volley.toolbox.Volley import org.json.JSONObject class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } val jsonobj = JSONObject() fun nextactivity(v:View){ jsonobj.put("username",username.text) jsonobj.put("password",password.text) val URL = "http://www.example.com/registeractivity.php" val que = Volley.newRequestQueue(this@MainActivity) val req = JsonObjectRequest(Request.Method.POST,URL,jsonobj, Response.Listener{ response -> Grobal.grobalexsist = response.toString() },Response.ErrorListener { Grobal.grobalexsist = "0" }) que.add(req) var nametext : String = username.getText().toString() var passtext : String = password.getText().toString() Grobal.grobalusername = nametext Grobal.grobalpassword = passtext val intent = Intent(this,RegisteredActivity::class.java) startActivity(intent) } fun loginactivity(v: View){ val intent = Intent(this,LoginActivity::class.java) startActivity(intent) } } 2つ目のファイル build.gradle apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 28 defaultConfig { applicationId "com.example.yusuke.mysql02" minSdkVersion 20 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'com.android.support:appcompat-v7:28.0.0-rc02' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.android.volley:volley:1.1.1' } 3つ目のファイル Manifestファイル <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.and

専門家に質問してみよう