背景色などを固定したとする。
style.xml
- <resources>
- <style name="BackStyle">
- <item name="android:background">#C8FFC8</item>
- <item name="android:textColor">#0066ff</item>
- </style>
- </resources>
androidManifest.xml(一部)
- <application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/BackStyle">
- <activity android:configchanges="orientation" android:label="@string/app_name" android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN">
- <category android:name="android.intent.category.LAUNCHER">
- </category>
- </action>
- </intent-filter>
- </activity></application>
これで背景色と文字色がアプリケーション単位で固定できるのですが、
うっかりToastまでこんなことになります。
Toastの透過の問題なのかどうだかしりませんが、
コードはこんな感じのいたって普通。
- Toast.makeText(this, "ほめてくれてありがとう", Toast.LENGTH_LONG).show();
この1行だけであの大惨事・・・。
リファレンスにも特にToastに関することはなにもかかれていない。
それでもなんとか、Toastに「setView()」なるものを発見したので
諦めて1つViewを作成することにしました。
といっても、XMLには詳しくないので
コードでゴリゴリと。
こんなメソッドを作成。
- public View makeToast(String message) {
- TextView text = new TextView(this);
- text.setBackgroundColor(Color.WHITE);
- text.setText(message);
- text.setPadding(5, 5, 5, 5);
- return text;
- }
これだとstring.xmlが使えなくてしょうもないので書き変えてみる。
- public View makeToast(int messageId){
- TextView text = new TextView(this);
- text.setBackgroundColor(Color.WHITE);
- text.setText(messageId);
- text.setPadding(5, 5, 5, 5);
- return text;
- }
で、実際にToastを使用するところでこうする。
- Toast toast = new Toast(this);
- toast.setView(makeToast("ほめてくれてありがとう"));
- toast.show();
string.xmlに定義している文字列を使うときはこうする。
- Toast toast = new Toast(this);
- toast.setView(makeToast(R.string.talk_atama1));
- toast.show();
要するにViewを継承しているTextViewを自前で作成して
ToastのViewとしてセットして表示しているだけです。
実際にはこんな感じのポップアップになります。
純粋なToastみたいな角丸なものにするには
もうちょっとこったものにしたりとかする必要があるのですが、
応急処置としてこちらをつくってみました。
いくらなんでも背景色にとけこんだToastがでてくるとがっかりだよね、ということで。
0 件のコメント:
コメントを投稿