どのように私は垂直方向にレイアウトを使用して、リスト内のアイテムを揃えていますか?

StackOverflow https://stackoverflow.com/questions/2451744

質問

私は、次の画像に画像やテキストのリストを表示するには、Android 1.5で、リストビューを使用しています。私は、テキストを垂直方向に中央揃えにしようとしていますが、テキストの代わりに中心の行の先頭にあります。以下は私のレイアウトがあります:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/row"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip">

    <ImageView android:id="@+id/item_image" android:layout_width="wrap_content" 
        android:layout_height="wrap_content"  android:paddingRight="10dip" 
        android:src="@drawable/default_image" android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_centerVertical="true"
        android:gravity="center_vertical"/>

    <TextView android:id="@+id/item_title" 
        android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:layout_toRightOf="@id/item_image" 
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_centerVertical="true"
        android:gravity="center_vertical"
        />

</RelativeLayout>

私が垂直方向にテキストを中央にしようとしているとき、私は、テキストも表示されないていない場合、私はalignParentTop =「true」に設定する必要がある奇妙なよう。私が間違って何をしているのですか?

役に立ちましたか?

解決

コメントを次EDITます:

これは、RelativeLayoutで、この作業を行うことが判明した簡単ではありません。答えの下部に私が望んでいた効果を与えるRelativeLayoutを含めましたが、それは、ListViewコントロールに含まれているだけになるまで。その後、質問に記載したのと同じ問題が発生しました。これは、代わりのLinearLayout(複数可)を使用して固定した。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:orientation="horizontal">

    <ImageView android:id="@+id/pickImageImage" 
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:background="@drawable/icon"
        android:scaleType="fitXY"
        android:layout_marginRight="10dp"/>

    <TextView android:id="@+id/pickImageText" 
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:gravity="left|center_vertical"
        android:text="I'm the text"/>

</LinearLayout>
その後、

あなたは2つのテキストボックスを持っているしたい場合は、次のことができImageViewの後に巣二orientation="vertical"とのLinearLayoutをし、そこにテキストボックスを配置します。

これは動作しますが、私はRelativeLayoutsはしなかった理由を私は知らない認めざるを得ません。たとえば、ロマン・ガイのことで、このブログ記事特にRelativeLayoutがすべきことを言います。私はそれを試したとき、私はそれが非常に動作するようになったことはありません。確かに彼がしたように私はそれの正確のをしませんでしたが、私の唯一の変更は、違いの多くを作っていてはいけませんTextViewsのいくつかの属性、とした。

<時間>

ここでは、元の答えがあります

私はあなたがRelativeLayout内のすべてのものをやや矛盾した命令でアンドロイドを混乱していると思います。私はこれまで、あなたの事を再フォーマットます:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/row"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip">

    <ImageView android:id="@+id/item_image"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingRight="10dip" 
        android:src="@drawable/icon"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"/>

    <TextView android:id="@+id/item_title" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@id/item_image" 
        android:layout_centerVertical="true"
        android:text="Blah!"/>

</RelativeLayout>

そして、それが正常に動作します。彼らは必要ありませんでしたので、私はあなたの冗長android:layout_alignParentxxxの多くを削除しました。このビューは現在左上隅にある絵と垂直隣に中央にテキストを思い付きます。絵の高さよりも、それ自体は何の背の高い作りませんしようとしているので、layout_heightは=「wrap_content」:あなたは絵が縦にも中心にしたい場合は、RelativeLayoutはアンドロイドになることはできません。あなたは、例えば、高さを指定する必要があると思います80dp、その後、アンドロイドで60dpのような固定の高さにImageViewのを設定します。それは適切に収まるように縮小させるためにscaleType =「fitXY」

他のヒント

しばらくの間、同様の問題に引っかかって、しかしCommonsWareからこれを発見された。

「あなたはレイアウトを膨らませるとき、親はリストビューでinflate(R.layout.whatever, parent, false)を、使用しています。」

作品がありますが、特定の値(つまり、あなたがwrap_contentを使用することはできません)に行の高さを設定するだけます。

ベースラインディレクティブはそれを行うだろうが、ImageViewのは、単純に、今日のように、ベースラインの位置合わせをサポートしていません。あなたは、ImageViewのサブクラスを作成することによってこの問題を回避getBaseline()メソッドをオーバーライドし、画像の高さを返すことができます。

scroll top