2012-03-06 6 views
0

선형 세로 레이아웃에서 내 활동에 세 가지보기가 있습니다. 위쪽 및 아래쪽 뷰에는 고정 된 높이가 있고 중간 뷰에는 사용 가능한 높이가 그대로 유지됩니다. 이것은 내가보기에 크기를 설정하는 방법입니다 다음 clientHeight를 얻기 위해Activity.onConfigurationChanged에서보기의 다시 그리기를 강제하는 방법

void resize(int clientHeight) 
{ 
    int heightMiddle = clientHeight - heightTop - heightBottom; 
    topView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, heightTop)); 
    middleView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, heightMiddle)); 
    bottomView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); 
} 

, 내가 된 onMeasure() 함수를 오버라이드 내 오버라이드 된 onMeasure() 함수 내에서() 크기를 호출합니다. 이것은 onCreate()에서 잘 작동합니다. 그러나 전화 방향이 변경되면 작동하지 않습니다. 내가 본 것은 onCreate() 후에 onMeasure()가 두 번 호출된다는 것입니다. onConfigurationChanged() 이후 onMeasure()는 한 번만 호출되며 크기 조정 코드는 적용되지 않습니다. 내 KLUGE 솔루션은 나중에() 20ms의 크기를 조정 호출 설정에 타이머 :

timer.schedule(new TimerTask() 
{ 
    @Override 
    public void run() 
    { 
     activity.runOnUiThread(new UiTask()); 
    } 
}, 20); 

UiTask 간단히) (크기를 호출 할 곳. 이것은 나를 위해 작동하지만 더 나은 솔루션이 있어야한다고 생각합니다. 누군가가 이것에 대해 밝힐 수 있습니까?

답변

1

android:layout_weight 속성을 사용하여 LinearLayout이 레이아웃을 수행하게하지 마시겠습니까? 그렇게하면 여분의 코드가 전혀 필요하지 않으며 모든 것이 제대로 작동합니다. 여기 고해상도/레이아웃/main.xml에의 모양은 다음과 같습니다

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:text="Top" 
     android:layout_weight="0" 
     android:background="#ff0000" 
     /> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="Middle" 
     android:layout_weight="1" 
     android:background="#00ff00" 
     /> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="25dp" 
     android:text="Bottom" 
     android:layout_weight="0" 
     android:background="#0000ff" 
     /> 
</LinearLayout> 

그리고 이외 더 이상 코드로 일반

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 
는 세로 같을 것이다

:

enter image description here

및 이와 유사 (자동으로 다시 출력되고 다시 그립니다) :

enter image description here

이 함께하고 매니페스트의 활동에 대한 android:configChanges="orientation"없이 모두 작동합니다. 필요한 경우 Java 코드를 사용하여 위의 레이아웃을 설정할 수도 있습니다.

+0

완벽한 솔루션입니다. 고맙습니다! –

관련 문제