【android】リスト項目のmatch_parentが効かない
公開日:
:
android
下記のようなリスト項目用のレイアウトを用意して、リストの右端にチェックボックスを置くようにしたのですが、TextViewのweight=1が効かずに、テキストの末尾にチェックボックスがくっついてしまう現象が起こりました。
<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="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/txt_listitem" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1" android:text="_title" /> <CheckBox android:id="@+id/cb_listitem" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
原因は。。。
原因はなんと親のListViewにありました。
下記が問題のListViewですが、間違いが分かりますか?
<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:layout_gravity="center" android:orientation="vertical" > <ListView android:id="@+id/list" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" > </ListView> </LinearLayout>
正解は。。。
修正個所はandroid:layout_widthのパラメータでした。
× android:layout_width=”wrap_content”
○ android:layout_width=”match_parent”
TextViewに長い文字列が入るとListViewは自然と画面横幅いっぱいに広がります。
これがmatch_parentの様な動作となるので気づくのに時間がかかってしまいました。
にしても、親ListViewの設定の如何によってリスト項目内部の表示が変わるのはやめて欲しいですね。
<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:layout_gravity="center" android:orientation="vertical" > <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > </ListView> </LinearLayout>
以上
関連記事
-
-
[android] JSONのパースにかかる時間
リストデータなんかを保存したいというのはよくある要件だと思います。 DBは面倒だしカラム毎に集計す
-
-
【android】Activityとプロセスのライフサイクル
またもやライフサイクル周りでハマったのですが、Androidのライフサイクルは複雑で困ります。 G
-
-
【android】設定画面の作成
res/xml/preferences.xml <?xml version=&quo
-
-
【android】 adbコマンド集
●adbサービス起動 adb start-server ●adbサービス終了 adb
-
-
【android】アプリのバージョン情報を取得する
int versionCode = -1; String versionName = &quo
-
-
【android】アプリ内課金を実装する
アプリ内課金のサンプルコードを解析します。 参考URL 公式API Android
-
-
【android】stringsリソースにパラメータを埋め込む
●strings.xml %n$x n : 引数に渡す際の順番。n番目の引数。 x
-
-
[android] ActivityとFragmentのライフサイクルいろいろ
Fragmentを使い始めてライフサイクル関係でハマることがあったので備忘録。 FragmentA
-
-
【android】非同期処理
Androidで非同期処理、マルチスレッドを処理するスニペット。 AndroidのスレッドはUIス
-
-
【android】複数のカスタムテーマを設定で切り替える
背景色やアプリ全体のテーマ色を設定で変更したいという要望をもらったので実装してみました。