Android 布局 wrap_content with layout_weight = 0 不显示

Android layout wrap_content with layout_weight = 0 does not show

我正在尝试创建一个布局,其中嵌入了 google 地图片段和地图下方选定标记的一些信息。我想让地图下面的信息只占用它需要的space,把剩下的space交给地图。

据我了解,layout_weight 将额外的 space 分配给与给定值成比例的不同小部件。因此,如果我有一个带有 layout_weight = 1 的小部件,另一个带有 layout_weight = 0 的小部件,第一个小部件将占用所有额外的 space,而第二个将只占用它需要的东西。所以如果我给第二个小部件layout_height = wrap_content,那么我应该得到我想要的。然而,这在实践中似乎并非如此。我已经更改了很多值,并且其中一个小部件占用了所有 space,或者两者都不占用。我能够让它们正常显示的唯一方法是将第二个小部件的 layout_height 设置为特定值。

这可能是问题的一部分,但我正在动态填充小部件 map_frag_event_iconmap_frag_personmap_frag_event_details


layout.xml

<?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="0dp"
    android:layout_margin="@dimen/fragment_margins"
    android:orientation="vertical">

    <fragment
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/map_frag_google_map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingBottom="@dimen/fragment_margins"
        tools:context=".fragment.MapFragment"/>

    <LinearLayout
        android:id="@+id/map_frag_event_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:orientation="horizontal"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/map_frag_event_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="@dimen/icon_padding"
            android:layout_weight="0" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center_vertical">

            <TextView
                android:id="@+id/map_frag_event_person"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_size"/>

            <TextView
                android:id="@+id/map_frag_event_details"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_size"/>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

strings.xml

<resources>
    <string name="app_name">Family Map</string>
    <string name="title_activity_maps">Map</string>

    <dimen name="fragment_margins">8dp</dimen>
    <dimen name="horizontal_layout_padding">8dp</dimen>
    <dimen name="vertical_layout_padding">4dp</dimen>
    <dimen name="icon_size">48sp</dimen>
    <dimen name="icon_padding">8dp</dimen>
    <dimen name="text_size">20sp</dimen>
    <dimen name="small_text_size">14sp</dimen>
    <dimen name="large_text_size">24sp</dimen>

    <dimen name="map_event_layout_height">120sp</dimen>
    <dimen name="map_event_icon_size">40sp</dimen>

</resources>

编辑

这是我正在寻找的一般设计,但我希望带有文本的白色 space 尽可能小,并且我想创建一个设置来更改文本大小,所以底部白色 space 应该是动态的。

如果可以,请解释发生了什么并提供解决方案。

在片段中保留 android:layout_weight="1" 但在 LinearLayout 中应用,例如 android:layout_weight="0.5"。检查结果,然后根据需要更改值。将其视为相关百分比,因此 1 到 0.5 将是 66% 到 33%

试试下面的布局:

    <fragment xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map_frag_google_map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/map_frag_event_layout"
        android:paddingBottom="@dimen/fragment_margins"
        tools:context=".fragment.MapFragment" />

    <LinearLayout
        android:id="@+id/map_frag_event_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/map_frag_event_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:padding="@dimen/icon_padding" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <TextView
                android:id="@+id/map_frag_event_person"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_size" />

            <TextView
                android:id="@+id/map_frag_event_details"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_size" />

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

试试下面的代码:

<?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:layout_margin="@dimen/fragment_margins"
    android:orientation="vertical">

    <fragment
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/map_frag_google_map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/map_frag_google_map"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingBottom="@dimen/fragment_margins"
        tools:context=".fragment.MapFragment" />

    <LinearLayout
        android:id="@+id/map_frag_event_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/map_frag_event_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="@dimen/icon_padding"
            android:src="@mipmap/ic_launcher" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <TextView
                android:id="@+id/map_frag_event_person"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_size" />

            <TextView
                android:id="@+id/map_frag_event_details"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_size" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

用这个替换你的 xml 代码,

<?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="0dp"
android:layout_margin="@dimen/fragment_margins"
android:orientation="vertical">

<fragment
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map_frag_google_map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.8"
    android:paddingBottom="@dimen/fragment_margins"
    tools:context=".fragment.MapFragment"/>

<LinearLayout
    android:id="@+id/map_frag_event_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"
    android:orientation="horizontal"
    android:gravity="center_vertical">

    <ImageView
        android:id="@+id/map_frag_event_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/icon_padding"
        android:layout_weight="0" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/map_frag_event_person"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="@dimen/text_size"/>

        <TextView
            android:id="@+id/map_frag_event_details"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="@dimen/text_size"/>

    </LinearLayout>

</LinearLayout>

您正在将两个视图的权重设置为 1。所以

我发现我发布的原始代码有两个问题。我不知道我在最初的测试中是什么时候添加的,但是有两个致命错误。在根 LinearLayout 中,我有 layout_height = "0dp",它应该是 layout_height = "match_parent"。在包含两个 TextViewsLinearLayout 中,我有 layout_height = "match_parent",它应该是 layout_height = "wrap_content"

修复了这两个错误后,@MuhammadMuzammilSharif 关于删除 layout_weight = "0" 的原始评论奏效了。


layout.xml

<?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:layout_margin="@dimen/fragment_margins"
    android:orientation="vertical">

    <fragment
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/map_frag_google_map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingBottom="@dimen/fragment_margins"
        tools:context=".fragment.MapFragment"/>

    <LinearLayout
        android:id="@+id/map_frag_event_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/map_frag_event_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="@dimen/icon_padding"/>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center_vertical">

            <TextView
                android:id="@+id/map_frag_event_person"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_size"/>

            <TextView
                android:id="@+id/map_frag_event_details"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_size"/>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>