2010-02-21 7 views
6

나는 다음과 같이 할 수있는 ListView 행을 얻으려고 다음하여 ImageButton으로ListView에 행 스타일링 - 왼쪽 정렬 텍스트, 오른쪽 정렬 아이콘

| Text-Text-Text      <ImageButton> | 

가 오른쪽 가장자리에 스냅. 어떻게해야합니까? 다음은 현재 사용중인 레이아웃 코드입니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layercontainer" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#699"> 
<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:layout_gravity="left"> 
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="YO HOW SI IT GOESSDA" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:layout_gravity="right"> 
    <ImageButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/trash" /> 
</LinearLayout> 
</LinearLayout> 

내 코드는 현재이 생산 : grrr

답변

16

단계 # 1하십시오 RelativeLayout베이스를 사용합니다.

단계 # 2 : 넣어하여 ImageButton로서 갖는 android:layout_alignParentRight="true"

단계 # 3 : 넣어하여 수직 정렬, 그리고 아마도 다른 RelativeLayout.LayoutParams 값 (...ImageButton의 ID이다) TextView 갖는다 갖는 android:layout_alignParentLeft="true", android:layout_toLeftOf="..."

+0

+1 : 여기

는 레이아웃 XML입니다. 아마도 RelativeLayout이 가장 적절할 것입니다. –

+0

초에 시도해보십시오. – GuyNoir

+0

안녕하세요! 그것은 효과가있다! 무리 감사. 이전에는 RelativeLayout을 사용한 적이 없었으므로 사용하지 않을 생각이었습니다. – GuyNoir

2

다음 코드 단편은 일부 텍스트 (가로로 왼쪽 맞춤, android:layout_alignParentLeft="true" 통해) 및 아이콘 (가로로 오른쪽으로 android:layout_alignParentRight="true") 및 모든 세로로 c 정렬 된 ListView 행을 만드는 방법 예를 제공합니다 entred (android:layout_centerVertical="true").

(전역 스타일에 따라 YMMV를) 다음과 같이 렌더 :

Rendering of code example

가장 오른쪽 아이콘의 왼쪽에 배치 할 수있는 주석 처리 된 추가 아이콘도 있습니다; 사용 가능하게 할 XML 주석 태그를 제거하십시오.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:descendantFocusability="blocksDescendants" 
    android:padding="6dp"> 
    <!-- Note: android:descendantFocusability="blocksDescendants" set to ensure that 
    OnItemClickListener works by ensuring constituent controls do not take focus --> 


    <TextView 
     android:id="@+id/lbl_list_item_row_text" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:lines="1" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:text="My List Item Text" 
     /> 

    <!-- This can be uncommented to add another button 

    <ImageButton 
     android:id="@+id/btn_additional_icon" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@+id/icon_additional_icon" 
     android:layout_centerVertical="true" 
     android:layout_width="wrap_content" 
     android:padding="3dp" 
     android:background="@null" 
     android:src="@drawable/icon_additional_icon" /> 

    --> 

    <ImageButton 
     android:id="@+id/icon_additional_icon" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:background="@null" 
     android:padding="3dp" 
     android:src="@drawable/icon_right_aligned_icon" /> 
</RelativeLayout>