首页 > 解决方案 > Xamarin Native Android LinearLayout 权重应用于儿童怪异问题

问题描述

我想要以下输出。我正在提供我的代码和一张显示其当前外观的图像。我正在将 MVVMCross 与适用于 Android 的 Xamarin Nativ UI 一起使用。

所需输出顶视图应占用 70% 的空间,下面应占用 30% 的可用空间。

当前布局在片段中使用。所以这个布局作为子元素添加到活动中的框架布局中,托管这个片段。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center|bottom"
        android:layout_weight=".7">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:text="Some Text"
            android:gravity="top"            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="Add Entry"
            local:MvxBind="Click AddCommand"            />
    </LinearLayout>


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".3">
        <ImageView
            android:src="@drawable/waves"
            android:adjustViewBounds="true"            
            android:layout_width="match_parent"
            android:layout_height="wrap_content"            />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/MilliliterViewContainer"/> <!-- The ml is a custom view which is being inflated and added in this layout from the code behind -->

    </FrameLayout>

</LinearLayout>

这是当前的输出,它以某种方式将权重应用于子图像视图。

当前输出我不明白为什么图像没有全宽显示?为什么将权重应用于图像视图宽度?波浪是矢量图像。

标签: androidandroid-layoutxamarin

解决方案


ImageViewAndroid 中的默认比例类型是fitCenter,这似乎告诉 Vector Drawable 刚好适合中心的高度。

如果切换到fitXY它将填充宽度。

我还建议您重新设计布局以ConstraintLayout代替使用,以避免在LinearLayout. 这可能看起来像:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:showIn="@layout/activity_main">

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.7" />

    <ImageView
        android:id="@+id/waves_image"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:src="@drawable/waves"
        android:scaleType="fitXY"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline" />

    <Button
        android:id="@+id/add"
        android:text="Add Entry"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintWidth_percent="0.6"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:id="@+id/overview"
        android:text="Overview Text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintWidth_percent="0.6"
        app:layout_constraintBottom_toTopOf="@+id/add"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</android.support.constraint.ConstraintLayout>

这显着扁平化了布局,意味着更少的透支和更少的计算布局。


推荐阅读