2011-05-16 5 views
3

위와 아래에 다른 뷰를 놓을 목적으로 ImageView를 화면 중간에 배치하려고하므로 LinearLayout (세로) 및 LinearLayout (가로). 그러나 ImageView에 layout_weight를 사용하면 이미지가 완전히 사라집니다 (빈 화면이 표시됨). ImageView의 이미지는 onCreate()에 설정됩니다. ImageView가 화면의 너비의 50 %를 차지하고 싶습니다. 따라서 가중치를 사용합니다.LinearLayout에서 ImageView와 함께 layout_weight 사용

<?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"> 

<View 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"/> 

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" android:orientation="horizontal" 
android:layout_weight="2"> 

<View android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"/> 

    <ImageView android:layout_weight="2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/albumArtImageView"></ImageView> 

    <View android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"/> 

</LinearLayout> 
<View 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"/> 
</LinearLayout> 

답변

5

에 두 번째있는 LinearLayout의 방향을 변경하려면 왼쪽 오버 할 공간이 필요합니다. 상단 LinearLayout 아래의 첫 번째보기는 layout_height="fill_parent"입니다. 그것은 모든 방을 차지합니다. 에 의해 결정되는 치수를 원하는 모든 위치에 layout_height="0dp"으로 설정하고 결과가 개선 될 것으로 생각하십시오.

+0

분명히 layout_height = "0"으로 설정할 때 정수형을 사용할 수 없습니다. – Alex

+0

0dp가 작동합니다. 고마워, 테드. – Alex

+0

Ack. 나는 항상 그 실수를 저지른다. 내 대답을 바로 잡아서 다른 누군가가 그걸로 넘어 가지 않게하려고합니다. –

-1

시도 아무것도 유용 할 layout_weight를 들어 수직

+0

이렇게하면 ImageView의 양쪽에 간격이 생기는 것을 방지 할 수 있습니까? 이것은 내가이 경우에 찾고있는 것이 아닙니다. – Alex