首页 > 解决方案 > Visual Studio 2017 Xamarin 需要使布局可滚动

问题描述

我有以下 axml 显然不起作用,因为我有多重线性布局。我需要使此布局可滚动,因为我需要捕获大量数据。我找不到任何工作。

<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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px"
    tools:ignore="HardcodedText">
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Parents Information"
                android:id="@+id/btnparents_info" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Medical Information"
                android:id="@+id/btnmedical_info" />
        </LinearLayout>
    </HorizontalScrollView>
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="175dp"
        android:layout_height="175dp"
        android:layout_gravity="center"
        android:id="@+id/imageview_participant" />
    <TextView
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/TextView_name" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="First Name" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/first_name"
            android:text="First Name" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Middle Initial" />
        <EditText
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:maxLength = "1"
            android:id="@+id/mi"
            android:text="M" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Last Name" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/last_name"
            android:text="Last Name" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="35dp"
            android:layout_marginLeft="10dp"
            android:text="Suffix" />
        <Spinner
            android:layout_width="110dp"
            android:layout_height="35dp"
            android:id="@+id/spinner_suffix" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Address" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/address"
            android:text="Address" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="City" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/city"
            android:text="City" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="State" />
        <Spinner
            android:layout_width="110dp"
            android:layout_height="35dp"
            android:id="@+id/state" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Zip Code" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLength = "5"
            android:id="@+id/zip"
            android:text="Zip Code" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Phone" />
        <MaskedEditText.MaskedEditText
            app:Mask="(###) ###-####"
            app:MaskFill="_"
            android:inputType="phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/phone"/>
       </LinearLayout>
    </ScrollView>
</LinearLayout>

如您所见,我在滚动视图中有多个线性布局,我认为这就是问题所在。我有几个观点我需要这样做。

** 更新 **

我将布局更改为此“

<ScrollView
    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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px"
    tools:ignore="HardcodedText">
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="First Name" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/first_name"
            android:text="First Name" />
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Middle Initial" />
        <EditText
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:maxLength = "1"
            android:id="@+id/mi"
            android:text="M" />
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Last Name" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/last_name"
            android:text="Last Name" />
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="35dp"
            android:layout_marginLeft="10dp"
            android:text="Suffix" />
        <Spinner
            android:layout_width="110dp"
            android:layout_height="35dp"
            android:id="@+id/spinner_suffix" />
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Address" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/address"
            android:text="Address" />
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="City" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/city"
            android:text="City" />
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="State" />
        <Spinner
            android:layout_width="110dp"
            android:layout_height="35dp"
            android:id="@+id/state" />
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Zip Code" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLength = "5"
            android:id="@+id/zip"
            android:text="Zip Code" />
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Phone" />
        <MaskedEditText.MaskedEditText
            app:Mask="(###) ###-####"
            app:MaskFill="_"
            android:inputType="phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/phone"/>
    </LinearLayout>
</ScrollView>

我得到的只是第一个Textview并排EditView,其余的根本不显示,但FindViewById显然可以找到它们。

标签: xamarin.androidvisual-studio-2017scrollview

解决方案


原因是 Scroll View 只能有一个直接子级:

解决方案只需将所有子布局包装到一个 LinearLayout

   <ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android: orientation="vertical"
     >
    // Add your multiple linear layouts here ans set orientation for above linear according to your requirement
     </LinearLayout></ScrollView>

祝你好运!

如果这不起作用,请还原

更新

我想让你做的是:

<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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px"
    tools:ignore="HardcodedText">
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Parents Information"
                android:id="@+id/btnparents_info" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Medical Information"
                android:id="@+id/btnmedical_info" />
        </LinearLayout>
    </HorizontalScrollView>
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="175dp"
        android:layout_height="175dp"
        android:layout_gravity="center"
        android:id="@+id/imageview_participant" />
    <TextView
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/TextView_name" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android: orientation="vertical"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="First Name" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/first_name"
            android:text="First Name" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Middle Initial" />
        <EditText
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:maxLength = "1"
            android:id="@+id/mi"
            android:text="M" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Last Name" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/last_name"
            android:text="Last Name" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="35dp"
            android:layout_marginLeft="10dp"
            android:text="Suffix" />
        <Spinner
            android:layout_width="110dp"
            android:layout_height="35dp"
            android:id="@+id/spinner_suffix" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Address" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/address"
            android:text="Address" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="City" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/city"
            android:text="City" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="State" />
        <Spinner
            android:layout_width="110dp"
            android:layout_height="35dp"
            android:id="@+id/state" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Zip Code" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLength = "5"
            android:id="@+id/zip"
            android:text="Zip Code" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Phone" />
        <MaskedEditText.MaskedEditText
            app:Mask="(###) ###-####"
            app:MaskFill="_"
            android:inputType="phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/phone"/>
       </LinearLayout>
         </LinearLayout>
    </ScrollView>
</LinearLayout>

推荐阅读