*

【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】stringsリソースにパラメータを埋め込む

●strings.xml %n$x n : 引数に渡す際の順番。n番目の引数。 x

記事を読む

no image

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

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

記事を読む

no image

アプリ間連携 Intentfiler

ブラウザの共有からURLを受け取る。 Manifest.xmlのURL受け取り先のactivity

記事を読む

no image

【android】設定画面の作成

res/xml/preferences.xml <?xml version=&quo

記事を読む

no image

【android】ネットワークの接続状況を確認する

コードから接続状況を確認。 ConnectivityManager co

記事を読む

no image

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

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

記事を読む

no image

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

またもやライフサイクル周りでハマったのですが、Androidのライフサイクルは複雑で困ります。 G

記事を読む

no image

【android】非同期処理

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

記事を読む

no image

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

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

記事を読む

no image

【andoid】ViewPagerを使う

1. layout.xmlにViewPagerを配置  2. PagerAdapter.jav

記事を読む

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 ↑