*

【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] モンキーテスト(Monkey Test)を実行する

最近テストの効率化に目覚めました。 モンキーテストは猿にアプリを渡してみてめちゃくちゃな操作をさせ

記事を読む

no image

【android】サービスの実装

ダウンロードなどActivityに依存したくない大きなバックグラウンド処理や常駐プロセスを作りたい場

記事を読む

no image

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

int versionCode = -1; String versionName = &quo

記事を読む

no image

【android】複数のカスタムテーマを設定で切り替える

背景色やアプリ全体のテーマ色を設定で変更したいという要望をもらったので実装してみました。

記事を読む

no image

【android】非同期処理

Androidで非同期処理、マルチスレッドを処理するスニペット。 AndroidのスレッドはUIス

記事を読む

no image

【android】 setOnClickListener(false)が効かない

間違いその1 OnClickListenerが登録されているViewにsetClickable

記事を読む

no image

【android】 Android4.1のserviceでdefaultPreferenceがおかしい

【現象】 ActivityでdefaultPreferencesで保存したデータがservice内

記事を読む

no image

【android】スリープモードに入らせない

Androidでは一定時間、操作をしないとスリープモードに入って待機状態になってしまいますが、動

記事を読む

no image

【android】任意のスレッドで処理を行う

非UIスレッドでUIを操作したい場合に、任意の処理をUIスレッド上で実行する。 目次

記事を読む

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 ↑