找不到“浮动操作按钮”

时间:2019-05-08 13:43:46

标签: c# android-layout xamarin.android

我有以下axml,其中包括一个浮动操作按钮,用于向用户提供说明。最初,由于滚动视图的原因,我无法使按钮正确浮动在屏幕的右下角,但在此处进行了修复:Floating action button over a scrollview stuck at top。现在按钮正确“浮动”了,FindViewById找不到了!不知道发生了什么,我该如何解决?

<android.support.design.widget.CoordinatorLayout 
    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:fitsSystemWindows="true">
<LinearLayout 
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <ImageView
                android:src="@drawable/Banner2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/imageView1" />
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_gravity="center"
                android:layout_marginTop="0sp"
                android:background="#000000" />
            <TextView
                android:text="New Account"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textView1"
                android:textAlignment="center" />
        </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" >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:text="Enter Username"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="150.5dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView1"/>
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/aTextboxUsername" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:text="Enter Password"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="150.5dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView1"/>
                    <EditText
                        android:inputType="textPassword"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/aTextboxPassword"
                        android:autoLink="none" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:text="Confirm Password"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="150.5dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView1"/>
                    <EditText
                        android:inputType="textPassword"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/aTextboxPassword2"
                        android:autoLink="none" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:text="Enter Email Address"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="192.5dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView1"/>
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/txtEmailAddress"
                        android:autoLink="none" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:text="Confirm Email Address"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="193.5dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView1"/>
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/txtEmailAddress2"
                        android:autoLink="none" />
                </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:text="First Name" />
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/firstname"
                         />
                </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:text="Middle Initial" />
                    <EditText
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:maxLength = "1"
                        android:id="@+id/mi"/>
                </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:text="Last Name" />
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/lastname"/>
                </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: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:text="Address" />
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/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:text="City" />
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/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: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:text="Zip Code" />
                    <EditText
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:maxLength = "5"
                        android:id="@+id/zip"/>
                </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:text="Contact Phone" />
                    <MaskedEditText.MaskedEditText
                        app:Mask="(###) ###-####"
                        app:MaskFill="_"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/phone"/>
                </LinearLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:layout_gravity="center"
                    android:layout_marginTop="0sp"
                    android:background="#000000" />
                <TextView
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:text="Select Company, Department, Section you are registering for." />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:layout_gravity="center"
                    android:layout_marginTop="0sp"
                    android:background="#000000" />
                <TextView
                    android:text="Select Company"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center" 
                    android:textStyle="bold"
                    android:id="@+id/textView2" />
                <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:id="@+id/company_spinner" />

                    <TextView
                    android:text="Select Department"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center" 
                    android:textStyle="bold"
                    android:id="@+id/textView2" />
                <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:id="@+id/department_spinner" />

                    <TextView
                    android:text="Select Section"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center" 
                    android:textStyle="bold"
                    android:id="@+id/textView2" />
                <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:id="@+id/section_spinner" />
                <Button
                    android:text="Submit"
                    android:layout_width="250px"
                    android:layout_gravity="center"
                    android:layout_height="34.5dp"
                    android:id="@+id/button_submit" />
            </LinearLayout>
        </ScrollView>
</LinearLayout>
 <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchorGravity="bottom|end"
    app:layout_anchor="@id/scrollView1"
    android:src="@drawable/alert_box" />
</android.support.design.widget.CoordinatorLayout>

*编辑*

FindViewById代码:

FloatingActionButton myFab = FindViewById<FloatingActionButton>(Resource.Id.fab);`

1 个答案:

答案 0 :(得分:0)

请删除项目中的“ bin”和“ obj”文件夹,然后重建项目。