나는 어떻게 세로로 정렬 내에서 항목 목록을 사용하여 상대적인 레이아웃?

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

문제

내가 사용하고 목록 보기에 안드로이드 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"때에 내가 하려고 수직으로 텍스트,하지만하지 않는 경우는 텍스트하지 않습니다.무엇이 잘못된 것입니까?

도움이 되었습니까?

해결책

편집을 다음과 같은 의견:

그것은 밝혀 만드는 이 작품으로 RelativeLayout 쉽지 않습니다.의 아래쪽에 답을 포함했다 RelativeLayout 을 제공하는 효과하고 싶었지만 그것은에 포함 ListView.그 후,동일한 문제에서 설명한 대로 질문에 발생했습니다.이 수정되었으로 대신 사용하여 LinearLayout(s).

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

하려면 두 개의 텍스트 상자할 수 있습니다 두 번째 orientation="vertical" 고 LinearLayout 후 이미지 뷰는 다음을 넣어 텍스트 상자에있다.

이 작품은,그러나 나를 인정하지 않아요 왜 RelativeLayouts 하지 않았다.예를 들어,이 블로그 게시물에 의해 사람 Romain 특별히 말하는 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 기 때문에 그들은 그가 필요 없다.이 보기에는 지금과 함께 사진을 왼쪽 상단 모서리에 텍스트와 수직 중심으로 다음니다.당신이 원하는 사진 수직 중심으로뿐만 아니라,당신은 있을 수 없습 RelativeLayout 에 안드로이드:layout_height="wrap_content"기 때문에 그 자체를 만들기 위해 노력하고 더 키 보다 높이의 그림이다.당신이해야 할 것을 지정하 높이는,예를 들어,80dp,그리고 다음 설정을 이미지 뷰 고정 높이 다음과 같 60dp 안드로이드:scaleType="fitXY"그것을 축소하는 적합니다.

다른 팁

한동안 비슷한 문제에 갇혀 있었지만 Commonsware에서 이것을 발견했습니다.

"레이아웃을 팽창시킬 때 사용하십시오 inflate(R.layout.whatever, parent, false), 여기서 부모는 ListView입니다. "

작동하지만 행의 높이를 특정 값으로 설정할 때만 작동합니다 (예 : Wrap_Content를 사용할 수 없습니다).

기준선 지침은이를 수행하지만 ImageView는 오늘날 기준선 정렬을 지원하지 않습니다. ImageView의 서브 클래스를 만들어 GetBaseline () 메소드를 재정의하고 이미지의 높이를 반환하여이 문제를 해결할 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top