在LinearLayout中使用layout_weight和ImageView

Ale*_*lex 4 android android-layout

我试图在屏幕中间放置一个ImageView,目的是在其上方和下方放置其他视图,因此我决定使用LinearLayout(垂直)和LinearLayout(水平).但是,当我对ImageView使用layout_weight时,图像完全消失(显示空白屏幕).ImageView的图像在onCreate()中设置.我希望ImageView占据屏幕宽度的50%,因此我使用了权重.

<?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>
Run Code Online (Sandbox Code Playgroud)

Ted*_*opp 5

为了layout_weight对任何东西都有用,需要有剩余的空间.您在顶部LinearLayout下的第一个视图有layout_height="fill_parent".占据了所有的空间.尝试layout_height="0dp"在任何地方设置你想要的维度layout_weight,我认为你的结果会更好.