- Date: Fri 27 04 2012
- Category: Android
- Response: Comment 0 Trackback 0
Android 3.x で View の高さ表示が他と違う
Android の複数のバージョンに対応したアプリを作っていたところ Android 3.x だけ高さの表示がおかしくなるという現象が発生しました。
どうなったかというと…
アイテムを2行表示するようにカスタマイズした ListView を作成した時に
( android.R.layout.simple_list_item_2 や android.R.layout.two_line_list_item を想像してください)
空文字が設定された行が表示されなくなってしまいました。
Android 2.x とAndroid 4.x は表示されるのに Android 3.x では表示されない…
本来であれば空文字が設定されても全て同じ高さの ListView が表示されるはずが Android 3.x の端末だけ空文字行が表示されないため、高さが疎らな ListView になってしまいました。
で、何が原因なのか TextView を使って検証してみました。
(検証内容は画面イメージ + XML を見てください)
【結果】
行に空文字が設定された場合…
Android 2.x と Android 4.x では、文字の高さの領域は確保される(横は確保されない)が
Android 3.x では文字の高さの領域は確保されない。
ただし、どのバージョンでも高さ(android:layout_height)に値を設定していればその分の領域は確保される。
つまり表示したかったら明示的に高さを設定しろ、ということのようです。
※android:text に何か文字(半角スペース可)を入れても正しく表示されます
他にも Spinner で試したところ縦位置が若干ずれて表示されました...
■ 画面イメージ -----------------------------------------------
↓ Android 2.2 (API 8)

↓ Android 3.2 (API 13)

※ 検証する TextView の設定値は wrap_content を基準とし
白枠に書かれた修正が加えられています
■ XML --------------------------------------------------------
どうなったかというと…
アイテムを2行表示するようにカスタマイズした ListView を作成した時に
( android.R.layout.simple_list_item_2 や android.R.layout.two_line_list_item を想像してください)
空文字が設定された行が表示されなくなってしまいました。
Android 2.x とAndroid 4.x は表示されるのに Android 3.x では表示されない…
本来であれば空文字が設定されても全て同じ高さの ListView が表示されるはずが Android 3.x の端末だけ空文字行が表示されないため、高さが疎らな ListView になってしまいました。
で、何が原因なのか TextView を使って検証してみました。
(検証内容は画面イメージ + XML を見てください)
【結果】
行に空文字が設定された場合…
Android 2.x と Android 4.x では、文字の高さの領域は確保される(横は確保されない)が
Android 3.x では文字の高さの領域は確保されない。
ただし、どのバージョンでも高さ(android:layout_height)に値を設定していればその分の領域は確保される。
つまり表示したかったら明示的に高さを設定しろ、ということのようです。
※android:text に何か文字(半角スペース可)を入れても正しく表示されます
他にも Spinner で試したところ縦位置が若干ずれて表示されました...
■ 画面イメージ -----------------------------------------------
↓ Android 2.2 (API 8)

↓ Android 3.2 (API 13)

※ 検証する TextView の設定値は wrap_content を基準とし
白枠に書かれた修正が加えられています
■ XML --------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="文字あり"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffff00"
android:text="TextView"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="文字なし"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffff00" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="android:layout_width=match_parent"
android:textColor="#000000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffff00" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="android:layout_height=match_parent"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#ffff00" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="android:layout_weight=30dp"
android:textColor="#000000" />
<TextView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:background="#ffff00" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="android:layout_height=30dp"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#ffff00" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="文字サイズ指定"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffff00"
android:textSize="30sp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="終わり"
android:textColor="#000000" />
</LinearLayout>
スポンサーサイト
このサイト用(正確には真面目な活動を行う用)に名前を変更しました。
とむ・やんくんです。
『とむ』と呼んでください。
この名前は本名の一部から取っています。
いままでの名前は本名とは全く関係ありませんでした。
名前を変えた理由は、ネット上での活動を区別するためです。
前の名前はネットで知人とアホな発言したりする際に、
新しい名前はプログラムやらやや真面目な話をする際に使用します。
とりあえずアホの子の名前(前回の名前)はこのブログ内から抹消しました。
これからは とむ として宜しく御願いします。
とむ・やんくんです。
『とむ』と呼んでください。
この名前は本名の一部から取っています。
いままでの名前は本名とは全く関係ありませんでした。
名前を変えた理由は、ネット上での活動を区別するためです。
前の名前はネットで知人とアホな発言したりする際に、
新しい名前はプログラムやらやや真面目な話をする際に使用します。
とりあえずアホの子の名前(前回の名前)はこのブログ内から抹消しました。
これからは とむ として宜しく御願いします。
- Date: Wed 18 04 2012
- Category: Android
- Response: Comment 0 Trackback 0
ファイル削除後のメディアスキャン:MediaScan
AndroidアプリからSDカード内のファイルを削除した時、ギャラリー内のサムネイルが消えないという問題が発生。
困った時のグーグル先生であっさり解決。
参考サイト:
アルパカの具 - Androidアプリで保存した画像をギャラリーに反映させる
Android Wiki - MediaScan
どうやらSDカードをマウント/アンマウントした時にシステムがSDカード内に格納されているメディアファイル(画像、音楽、動画等)をスキャンしてデータベース化しているらしい。
これを MediaScan と言う。
File#delete() などでファイルを削除した時はこの MediaScan は実行されない。
明示的に MediaScan を実行するための Android API は用意されていないが
Broadcast 送信を行うことで MediaScan を呼ぶことができる。
ウイルス検知アプリが入っている状態でこの MediaScan を実行した場合、検知アプリが実行されてしまうので注意が必要。
困った時のグーグル先生であっさり解決。
参考サイト:
アルパカの具 - Androidアプリで保存した画像をギャラリーに反映させる
Android Wiki - MediaScan
どうやらSDカードをマウント/アンマウントした時にシステムがSDカード内に格納されているメディアファイル(画像、音楽、動画等)をスキャンしてデータベース化しているらしい。
これを MediaScan と言う。
File#delete() などでファイルを削除した時はこの MediaScan は実行されない。
明示的に MediaScan を実行するための Android API は用意されていないが
Broadcast 送信を行うことで MediaScan を呼ぶことができる。
// SDカードのパスを取得する
String path = "file://"
+ Environment.getExternalStorageDirectory();
// メディアスキャンを実行する
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED, Uri.parse(path)));
ウイルス検知アプリが入っている状態でこの MediaScan を実行した場合、検知アプリが実行されてしまうので注意が必要。