2012-03-01 1 views
6

나는 ListView에서 이상한 행동을하고 있습니다.android의 ListView 행의 동적 높이

일부 데이터가 채워진 ListView가 있습니다. 각 행에는 이미지와 세 개의 텍스트 레이블이 있습니다. 다음 행에 대한 내 XML이야 (I은 어댑터의 getView 방법에 올리는거야)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:paddingTop="0dip" 
    android:paddingBottom="0dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <ImageView android:id="@+id/Icon" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     android:padding="2dp" 
     android:scaleType="fitCenter" 
     android:contentDescription="@string/app_icon"/> 

    <TextView android:id="@+id/TxtName" 
     android:scrollHorizontally="false" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:textColor="@android:color/black" 
     android:background="@color/list_bg1" 
     android:layout_weight="2" 
     android:padding="2dp"/> 

    <TextView android:id="@+id/TxtPackage" 
     android:scrollHorizontally="false" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="2" 
     android:textColor="@android:color/black" 
     android:background="@color/list_bg2" 
     android:padding="2dp"/> 

    <TextView android:id="@+id/TxtSource" 
     android:scrollHorizontally="false" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="2" 
     android:background="@color/list_bg3" 
     android:textColor="@android:color/black" 
     android:padding="2dp"/> 
</LinearLayout> 

을 이제, 텍스트 필드에 들어가는대로 (80 ~ 100 자 몇 자에서) 어떤 아이폰에의 할 수있다 래핑 된 라벨의 높이를 수용하기 위해 라벨을 단지 감싸고 행의 높이를 확장하고 싶습니다.

그러나 레이블의 줄이 잘리지 만 행의 높이가 확장되지 않으므로 레이블의 아래쪽이 잘립니다. 모든 내용에 맞게 자동으로

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    SetInfo app = (SetInfo)getItem(position); 
    if(app == null) 
     return null; 

    LinearLayout row = (LinearLayout) (convertView == null 
       ? LayoutInflater.from(context).inflate(R.layout.listitem, parent, false) 
       : convertView); 
    ((TextView)row.findViewById(R.id.TxtName)).setText(app.getName()); 
    ((TextView)row.findViewById(R.id.TxtPackage)).setText(app.getPackage()); 
    ((TextView)row.findViewById(R.id.TxtSource)).setText(app.getSource()); 
    if(app.getIcon() != null) 
     ((ImageView)row.findViewById(R.id.Icon)).setImageDrawable(app.getIcon()); 
    return row; 
} 

가 어떻게 서로 다른 높이로 행을 가질 수 조정 : 여기 내 getView 방법입니까?

+0

더 나은 방법은 android : maxLines = 1 행 레이아웃에서 Textview에서 한 줄 이상의 텍스트 방지 –

+0

android : layout_weight를 1 또는 그 이상으로 조정 해 보셨습니까? –

+0

@Samir : 정확한 반대를 원합니다. 모든 텍스트를 표시해야하므로 레이블을 반드시 포장해야합니다. –

답변

8

android : layout_height = "wrap_content"로 TextView를 변경하십시오.

+0

이것은 거의 내가 원하는 것을 수행합니다. 즉, 행의 높이가 레이블의 줄 바꿈을 허용하도록 확장되지만 다른 배경 색을 다른 레이블로 설정하여 열을 올바르게 색칠하는 기능이 느슨합니다. 이제 색상은 레이블이 감싸는 범위까지만 확장되며 다른 레이블이 더 많은 줄로 감싸이면 색상이 지정된 레이블 아래에 흰색 기본 배경이 표시됩니다. 어떤 아이디어를 어떻게 해결할 수 있습니까? –

+0

이 대답은 결국 내가해야 할 일이므로 받아 들일 것입니다. 그런 다음 원하는 방식으로 레이아웃을 얻으려면 다른 고통스러운 처리 작업을 계속 진행하십시오. –

4

샘 문제가있어서 @ maebe의 대답이 나에게 도움이되지 못했습니다. 그것을 작동 시키려면 어댑터의 getView 내 TextView에 requestLayout()을 호출해야했습니다.

따라서, 귀하의 경우, 각 텍스트 뷰에 대한() requestLayout를 호출해야합니다 :

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    // Inflate your view etc. 

    row.findViewById(R.id.TxtName).requestLayout(); 
    row.findViewById(R.id.TxtPackage).requestLayout(); 
    row.findViewById(R.id.TxtSource).requestLayout(); 
    return row; 
} 

나는 모든 종류의 트릭을 시도하고이 내 사건에서 일을하는 유일한 하나입니다.

+0

+1이 해킹입니다. 내 하루를 만들어. 고마워요 @ muscardinus – niranjan94