*

【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】複数のカスタムテーマを設定で切り替える

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

記事を読む

no image

【android】リソースからいろいろ取得する

リソースで定義したいろいろをコード内で呼び出す方法です。 レイアウト //リソースからレ

記事を読む

no image

【android】標準の設定画面を作る

設定画面のテンプレートです。 検索一発クンのコードから抜粋しました。 チェックボックス、リス

記事を読む

no image

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

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

記事を読む

no image

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

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

記事を読む

no image

【android】アニメーション

Viewにアニメーションを付加する方法です。 目次 アニメーションの実行 de

記事を読む

no image

【android】処理時間を計測する

パフォーマンスのリファクタリングを行うには処理時間の計測がかかせません。 SDK標準のクラスを使っ

記事を読む

no image

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

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

記事を読む

no image

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

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

記事を読む

no image

[android] モンキーテスト(Monkey Test)を実行する

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

記事を読む

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 ↑