视图内的 LinearLayout (Xamarin.Android)

问题描述

我想在 View(代表一个矩形)中放置一个 LinearLayout,以便在这个 Layout 中放置更多元素。不幸的是,这在某种程度上不起作用,当我将它们放入视图时,它只是无法识别 LinearLayout 或其他元素。

我在 Visual Studio Community 2019 中使用 Xamarin.Android(仅限 Android,而非 iOS)。

这是 XML 代码。 @drawable/rounded 只是一个 .xml 文件,它使视图成为一个带有圆角的矩形。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:background="#ff74a8a7"
    android:orientation="vertical">

    <!--Header-->

    <TextView
        android:text="MyTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        android:gravity="center"
        android:id="@+id/txtTitle"
        android:textColor="#ff2f5858"
        android:textStyle="bold" />

    <!--View-->
    <View
        android:layout_width="300dp"
        android:layout_height="450dp"
        android:layout_marginTop="50dp"
        android:layout_gravity="center"
        android:background="@drawable/rounded"
        android:backgroundTint="#ff38817a" >

        <!--Here should the next LinearLayout go,e.g.: (does not work!-->
        <LinearLayout>
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!--Elements inside this LinearLayout,e.g. a text and a button-->

        </LinearLayout>

    </View>

</LinearLayout>

将不胜感激有关如何解决此问题的帮助!

解决方法

布局的这部分不起作用:

<View
    android:layout_width="300dp"
    android:layout_height="450dp"
    android:layout_marginTop="50dp"
    android:layout_gravity="center"
    android:background="@drawable/rounded"
    android:backgroundTint="#ff38817a" >

    <!--Here should the next LinearLayout go,e.g.: (does not work!-->
    <LinearLayout>
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!--Elements inside this LinearLayout,e.g. a text and a button-->

    </LinearLayout>

</View>

View 不是 ViewGroup,并且可以根据设计没有任何子项。您可以使用 FrameLayoutLinearLayoutRelativeLayoutConstraintLayout 或任何其他布局来托管多个子项。

您可以删除外部视图,您应该能够看到您的 LinearLayout