【问题标题】:LinearLayout inside a View (Xamarin.Android)视图内的线性布局 (Xamarin.Android)
【发布时间】:2021-07-25 12:19:21
【问题描述】:

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

我在 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>

我们将不胜感激如何解决这个问题!

【问题讨论】:

  • 能否请您提供您正在创建的布局,以便我们帮助您弄清楚发生了什么?
  • @Cheesebaron 我在主帖中添加了 XML 代码,感谢您的帮助!

标签: c# android xamarin xamarin.android android-view


【解决方案1】:

这部分布局不起作用:

<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

【讨论】:

  • 您好,首先感谢您的重播。问题是,我希望 View 在那里,或者任何其他可以显示圆角矩形的东西。在这个矩形内,LinearLayout 应该和它的孩子一起去。
  • 您只需将View 更改为FrameLayout 即可获得所需的效果。但是,对于圆角,您甚至不需要另一个嵌套级别。您只需应用 OutlineProvider 并允许剪辑视图。
猜你喜欢
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-19
  • 1970-01-01
相关资源
最近更新 更多