2013-09-29 5 views
4

나는 내 안드로이드 레이아웃과 같이 행동 할 :안드로이드 ViewGroup과 그 모든 자식 뷰를 가장 넓은 자식을 기준으로 동등한 너비를 동적으로 유지하려면 어떻게해야합니까?

|-------SomeLayout-------| 
||--------Widget1-------|| 
||______________________|| 
||--------Layout2-------|| 
||______________________|| 
||--------Widget2-------|| 
||______________________|| 
|________________________| 

3 자녀 유무 : 한다고 가정 SomeLayout 지금처럼 위에서 아래로 순서대로 표시됩니다 WIDGET1, 레이아웃 2, 및 WIDGET2의 부모입니다 변화하는 콘텐츠가 있습니다. 그들이 변화함에 따라 동적으로, 나는 항상 가장 넓은 내용을 담은 내용을 내용으로 포장하기를 원합니다. SomeLayout이 가장 넓은 아이를 감싸고 싶습니다. 다른 모든 아이들이 그 가장 넓은 아이와 일치하도록합니다. 어느 것이 가장 큰 변화라도.

저는 제가 생각할 수있는 것보다 많은 방법을 사용하여이 작업을 수행하려고 노력해 왔습니다. 모든 실패. 누구든지 안드로이드 레이아웃 XML을 통해 이러한 효과를 얻으실 수 있습니다. 누구든지 제공 할 수 있다면 Java 프로그램 방식의 솔루션을 기꺼이 사용 하겠지만 XML을 통해 수행하는 것을 선호합니다.

답변

-1

좋은 질문 친구. 이 후

그것의 단순한 하나 .. 이것 좀 봐 .. 여기

당신이 원하는 것입니다 .. 나는 이것이 좋은 질문 느꼈다 검사를 시작 .. 는 wrap_content 몇 가지 레이아웃 폭을 적용하고 모든 match_parent에 자식의 너비를 직접 지정하십시오.

아래 예제를 참조하십시오. 배경색을 준수 할 수 있습니다.

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

    <!-- This is Your Some Layout --> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <!-- Widget1 --> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="2431234vb sset " /> 

     <!-- Your Layout 2 (inner one) --> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="2431234v " /> 
     </LinearLayout> 

     <!-- Widget2 --> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="2431234vb sset " /> 
    </LinearLayout> 

</LinearLayout> 

그것은 ..

0

나는 당신의 SomeLayout이 뷰 그룹을 확장 희망, 그래서 적절한 방법의 모든 차일을 측정하기 위해 방법을 된 onMeasure 무시하려고 줄 당신에게 도움이되기를 바랍니다.

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int specWidth = MeasureSpec.getSize(widthMeasureSpec); 
    int maxWidth = 0; 
    int maxIndex = 0; 
    for (int i = 0; i < getChildCount(); i++){ 
     View child = getChildAt(i); 
     final int widthSpec = MeasureSpec.makeMeasureSpec(
        specWidth, MeasureSpec.UNSPECIFIED); 
     final int heightSpec = MeasureSpec.makeMeasureSpec(
        0, MeasureSpec.UNSPECIFIED); 
     child.measure(widthSpec, heightSpec); 
     int width = child.getMeasuredWidth(); 
     if (width > maxWidth){ 
      maxWidth = width; 
      maxIndex = i; 
     } 
    } 
    for (int i = 0; i < getChildCount(); i++){ 
     if (i != maxIndex){ 
      View child = getChildAt(i); 
      final int widthSpec = MeasureSpec.makeMeasureSpec(
        maxWidth, MeasureSpec.EXACTLY); 
      final int heightSpec = MeasureSpec.makeMeasureSpec(
        0, MeasureSpec.UNSPECIFIED); 
      child.measure(widthSpec, heightSpec); 
     } 
    } 
    setMeasuredDimension(someMeasuredWidth, someMeasuredHeight); 
} 

예를 들어 그래서 당신은 가장 넓은 아이를 정의하고 최대 크기를 조정하기 위해 다른 모든 측정해야한다. 그 후에 측정 된 치수에 따라 onLayout 메소드 구현의 모든 자식을 레이아웃해야합니다.

가 당신에게 내가 같은 높이의 모든 차일 레이아웃을 만들 수있는 방법을 찾고 여기 온

+0

도움이 될 것입니다 희망 나는 당신의 방법을 사용하여 그렇게하여 수행하는 관리, 그러나 나는 (someMeasuredWidth을 마지막 줄'setMeasuredDimension을 삭제했다 , someMeasuredHeight); 그리고 두번째 줄에 추가했다. \t'super.onMeasure (widthMeasureSpec, heightMeasureSpec);' – Epaminondas