2013-06-15 5 views
11

레이아웃에 두 개의보기가 있습니다. 전화 번호는 각각 View AView B입니다.형제보기가 더 큰 경우 MATCH_PARENT, 형제보기가 더 작은 경우 WRAP_CONTENT

┌──────┐ 
│┌─┐┌─┐│ 
││A││B││ 
│└─┘└─┘│ 
└──────┘ 

(View AView B 포함) 상위 레이아웃의 높이 WRAP_CONTENT이다.

여기서, View B의 높이는 WRAP_CONTENT입니다. 즉, 내용의 높이에 따라 높이를 변경할 수 있습니다. 내가 원하는 무엇

View A의 내용의 내용이 View B보다 짧은 '경우의 높이

  1. 세트 View A'View B까지의 높이 '입니다.
  2. View A의 콘텐츠가 View B의 콘텐츠보다 크면 View A 님의 콘텐츠 높이까지 설정하십시오. View B의 내용이 높다 경우

그래서,

① 다음 View A의 높이의 높이가 View B로 설정됩니다 '. View B의 내용이 짧은 경우

 ┌──────┐  ┌──────┐ 
     │┌─┐┌─┐│  │┌─┐┌─┐│ 
     ││ ││ ││  ││A││ ││ 
I want ││A││B││, not │└─┘│B││. 
     ││ ││ ││  │ │ ││ 
     │└─┘└─┘│  │ └─┘│ 
     └──────┘  └──────┘ 

은 ②, 다음 View A 자신의 conent의 높이의 높이 View A이다 '. View A 설정

 ┌──────┐  ┌──────┐ 
     │┌─┐┌─┐│  │┌─┐┌─┐│ 
     ││ ││B││  ││A││B││ 
I want ││A│└─┘│, not │└─┘└─┘│. 
     ││ │ │  └──────┘ 
     │└─┘ │ 
     └──────┘ 

부모 LinearLayout (Horizontal) 경우

, MATCH_PARENT까지의 높이를 위반하는 경우 2.

부모 RelativeLayout 경우 정렬 View A 설정 'WRAP_CONTENT까지의 높이가 케이스 (1) 및 View A 설정 위배' 상위 상위와 하위 모두가 의 조건을 위반합니다. Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.

이 문제를 어떻게 해결할 수 있습니까?

답변

6

사용자 정의 ViewGroup을 작성하는 등 여러 가지 방법이 있습니다. 수평면은 LinearLayout 일 것입니다. 그러나 런타임시 요구 사항을 동적으로 설정하는 것이 가장 직접적인 해결책이라고 생각합니다.

수평 LinearLayout에 두 TextView의 다음과 같은 레이아웃을 고려 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/container" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#f00" 
    android:padding="10dp" > 

    <TextView 
     android:id="@+id/first" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#0f0" 
     android:padding="5dp" 
     android:text="One line" /> 

    <TextView 
     android:id="@+id/second" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#00f" 
     android:padding="5dp" 
     android:text="Two\nlines" 
     android:textColor="#fff" /> 

</LinearLayout> 

모두 기본적으로 첫 번째 요구 사항까지 일치하는 자신의 높이를 포장.

요구 사항 2 : 데모 목적으로 두 번째 줄을 두 줄로 나눠 첫 번째 것보다 더 크게 만듭니다. 당신이해야 할 모든이, 그것은 높이의 첫 TextView 경기를하려면 :

'트릭'는 아이들이 측정 될 때까지 실행되지 않습니다 논리를 보장 뷰의 큐에 게시하는 것입니다
LinearLayout container = (LinearLayout) findViewById(R.id.container); 
final TextView first = (TextView) findViewById(R.id.first); 
final TextView second = (TextView) findViewById(R.id.second); 

container.post(new Runnable() { 
    @Override public void run() { 
     int h1 = first.getMeasuredHeight(); 
     int h2 = second.getMeasuredHeight(); 
     if (h1 < h2) first.getLayoutParams().height = h2; 

    } 
}); 

및 유효한 높이를 가졌다. 요구 사항 2의 경우 첫 번째 뷰의 높이는 두 번째 뷰가 더 커야 만 변경 될 수 있습니다. 이는 정확히 run() 메서드에서 파트가 수행하는 것입니다.

+0

이 작업을 수행 할 XML 방법이 없습니까? 그렇지 않으면 내가 동적으로 채워진 목록 안에 이런 식으로 뭔가하고 싶어하기 때문에 내 어댑터 내 에서이 일을해야합니다. –

+1

@BartBurg : 커스텀'ViewGroup'이 아마도 가장 최적의 솔루션 일 것입니다. 'LinearLayout'을 확장하는 것은 좋은 출발점이 될 것이며, 특정 유스 케이스에 따라 위에서 설명한 기준을 고려하기 위해'onMeasure()'를 오버라이드해야합니다. –

+0

애니메이션 효과가 발생하여 바람직하지 않을 수 있습니다. 사용자 정의 ViewGroup 샘플 크게 감사하겠습니다. –

1

레이아웃 매개 변수를 사용하여 프로그래밍 방식으로 뷰의 크기를 설정해야합니다. 당신이 당신이 할 것 인 것이다 견해를 유지하기 위해있는 LinearLayout을 사용하고 있다고 가정하면

//the views are called a and b 
LinearLayout.LayoutParams aParams = a.getLayoutParams(); 
LinearLayout.LayoutParams bParams = b.getLayoutParams(); 
int aWidth = aParams.width; 
int aHeight = aParams.height; 
int bWidth = bParams.width; 
int bHeight = bParams.height; 
if (aHeight >= bHeight) { 
    // do nothing 
} 
else { 
    a.setLayoutParams(new LinearLayout.LayoutParams(aWidth, bHeight)); 
} 

당신이 그들의 높이가 원래 이전이 수정에, XML에서 wrap_content로 유지 설정합니다.

0

이것은 axml에서 아주 쉽게 해결할 수 있습니다.

부모 레이아웃의 높이를 wrap_content로 유지하고 모든 하위 뷰의 높이를 match_parent로 설정합니다. 이렇게하면 상위 레이아웃의 높이가 가장 높은 하위로 래핑되고 모든 하위 뷰의 높이가 부모로 늘어납니다.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="text" /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:text="More\ntext" /> 
</LinearLayout> 
관련 문제