gpt4 book ai didi

android - 在 LinearLayout 中将 layout_weight 与 ImageView 结合使用

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:15:30 25 4
gpt4 key购买 nike

我试图将一个 ImageView 放置在屏幕中间,目的是将其他 View 放在它的上方和下方,因此我决定使用 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>

最佳答案

要使 layout_weight 对任何东西都有用,就需要有剩余空间。您在顶部 LinearLayout 下的第一个 View 具有 layout_height="fill_parent"。那占据了整个房间。尝试在您希望尺寸由 layout_weight 确定的任何地方设置 layout_height="0dp",我认为您的结果会更好。

关于android - 在 LinearLayout 中将 layout_weight 与 ImageView 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6019082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com