2013-10-20 3 views
2

하나의 listview가있는 하나의 상대보기가 있습니다. 나는이 상대적인 뷰를 런타임에 만들고 내용을 (기본적으로 문자열 배열) 내 목록보기에 추가합니다. 나는 "wrap_content"listview 너비와 높이를 설정했습니다. 문제는 내 목록 부모보기가 내 목록보기를 표시하기 위해 크기가 조정되지 않습니다. 단지 목록의 첫 번째 값을 보여줍니다. onmeasure 메서드를 재정의해야한다는 것을 알고 있습니다. 그래서, 정확히 어떻게 onmeasure 메서드에서 내 listview의 높이를 얻을 수 있도록 부모님보기로 전달할 수 있습니다. 나는이 같은 많은 문제를 읽었지만 작동시키지 못했습니다.부모보기의 높이를 자식보기에 맞게 변경하는 방법

또 하나의 getMeasuredHeight()는 onmeasure() 메소드에서 0을 반환합니다.

업데이트

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/layout_route_search" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<RelativeLayout 
    android:id="@+id/layout_main" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</RelativeLayout> 

</ScrollView> 

여기 lo_block.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Text_view" /> 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

</RelativeLayout> 

내 코드입니다 activity_main 내 주요 레이아웃
Frag.java

public class Frag extends Fragment { 

private View mView; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    mView = inflater.inflate(R.layout.activity_main, null); 

    showView(); 
    return mView; 
} 

public void showView() { 

    block card = new block(getActivity()); 

    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, 
      LayoutParams.WRAP_CONTENT); 

    card.setLayoutParams(params); 
    ViewGroup vg = (ViewGroup) mView.findViewById(R.id.layout_main); 
    vg.addView(card); 
} 

private class block extends RelativeLayout { 

// int listViewh = 0; // private float listViewHeight; (H, w)

public block(Context context) { 
     super(context); 

     View mView = View.inflate(getContext(), R.layout.lo_block, this); 

     ListView lst = (ListView) mView.findViewById(R.id.listView1); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(
       getActivity(), android.R.layout.simple_list_item_1, 
       new String[] { "ad", "sfsd", "sfsf", "asfsfs" }); 

     lst.setAdapter(adapter); 
    } 

    public block(Context context, AttributeSet attrs, int defStyle) { 

     super(context, attrs, defStyle); 

    } 

    @Override 
    public int getMinimumHeight() { 

     return super.getMinimumHeight(); 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 

    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     int w = MeasureSpec.getSize(widthMeasureSpec); 
     int h = MeasureSpec.getSize(heightMeasureSpec); 

// setMeasuredDimension; }

} 

}

main.xml에

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/fragment" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

</LinearLayout> 

내 주요 활동 공용 클래스 MainActivity 내 경우 활동 {

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    getFragmentManager().beginTransaction().replace(R.id.fragment, 
      new Frag()).commit(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 


} 
+0

Relativelayout의 너비와 높이도 wrap_content입니다. –

+0

결과도 마찬가지입니다. 메모의 경우, 일부 픽셀에 대한 내 listview의 높이를 설정하면 "250dp"라고 표시되는데, 내 상대 레이아웃은 그 자체가 렌더링되기 전에 상대적인보기에 계산 높이를 부여하는 유일한 문제를 보여줍니다. –

+0

정확하지는 않지만 정상적인 활동에는 문제가 없지만이 코드를 조각에 넣으면 작동하지 않습니다. 단편과 함께 한 번 시도해보십시오. 주요 활동 xml은 선형 레이아웃을 사용하여 하나의 프레임을 출력합니다. –

답변

1

정확한 문제를 확장 나는 노력하고있다 sc30 안에 listview 넣기 보기. 및 thats 권장하지 않음 일반적으로 그러나 나는 목록보기를 확장 할 수 있었다. 그 스크롤은 이전보다 좋았지 만. 아래는 생성자에서 추가 한 코드입니다.

ListView lst = (ListView) mView.findViewById(R.id.listView1); 
     String[] arrayList = new String[] { "ad", "sfsd", "sfsf", "asfsfs", 
       "ssfsff", "llkllk", "4323" }; 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(
       getActivity(), android.R.layout.simple_list_item_1, 
       arrayList); 
     int totalHeight = 0; 
     for (int i = 0; i < adapter.getCount(); i++) { 
      View listItem = adapter.getView(i, null, lst); 
      listItem.measure(0, 0); 
      totalHeight += listItem.getMeasuredHeight(); 
     } 
     LayoutParams lp = (LayoutParams) lst.getLayoutParams(); 
     int height = totalHeight; 

     // arraylist list is in which all data is kept 

     lp.height = height; 
     lst.setLayoutParams(lp); 

     lst.setAdapter(adapter); 
관련 문제