*

【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>

以上

関連記事

no image

【android】アプリ内課金を実装する

アプリ内課金のサンプルコードを解析します。 参考URL 公式API Android

記事を読む

no image

【android】オーバーレイでトップレイヤーにViewを表示する

常にホーム画面や他のアプリより前面にViewを表示する方法です。 前面に透明のViewGroupを

記事を読む

no image

ダイアログを表示する

カスタムアラートダイアログ private void showCustomAlert(A

記事を読む

no image

【android】DrawableとBitmap、リソースの相互変換 

Resource → Bitmap Bitmap bm = BitmapFactory.dec

記事を読む

no image

【android】アプリのバージョン情報を取得する

int versionCode = -1; String versionName = &quo

記事を読む

no image

[android] ActivityとFragmentのライフサイクルいろいろ

Fragmentを使い始めてライフサイクル関係でハマることがあったので備忘録。 FragmentA

記事を読む

no image

【android】webviewでアプリ内にwebページを読み込む

webviewを使ってandroidアプリ内にwebページを読み込む定型文です。 //vie

記事を読む

no image

【android】ハードウェアキー入力を検出する

Activityを継承したクラスで下記を記述します。 public boolean

記事を読む

no image

[android] JSONのパースにかかる時間

リストデータなんかを保存したいというのはよくある要件だと思います。 DBは面倒だしカラム毎に集計す

記事を読む

no image

【android】 年月だけのDatePicker

final DatePicker datePicker = new DatePicker(sel

記事を読む

Message

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

no image
知らないと損をする6つのライセンスまとめ

オープンソースやフリーウェア、フリー素材などが巷に溢れ、それらを利用す

no image
ガリレオ:ニュースブラウザをリリースしました。

概要 ガリレオはニュースを読んだり、検索する機能に特化したブラウザア

no image
【android】Activityとプロセスのライフサイクル

またもやライフサイクル周りでハマったのですが、Androidのライフサ

→もっと見る

PAGE TOP ↑