c# - 有些 View 元素不会隐藏,有些不会出现

标签 c# android xamarin visual-studio-2017

出于某种原因,当我将可见性设置为 Gone 时,我的一些 View 元素不会隐藏。 ,当我将其设置为Visible时,其他人不会出现.

在这个例子中,cmdCallMe 不会隐藏,cmdOk 和 cmdCancel 也不会出现。其他元素的行为均符合预期。

(来源略有缩短)

Activity 代码:

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.p2);
        titleStatusView = FindViewById<TextView>(Resource.Id.lblP2Status);
        outputTextView = FindViewById<TextView>(Resource.Id.lblP2Timer);
        lblConfirm = FindViewById<TextView>(Resource.Id.lblP2Confirm);
        cmdP2Update = FindViewById<Button>(Resource.Id.cmdP2Update);
        cmdP2Arrived = FindViewById<Button>(Resource.Id.cmdP2Arrived);
        cmdP2SelfDeployed = FindViewById<Button>(Resource.Id.cmdP2SelfDeployed);
        cmdP2CallMe = FindViewById<Button>(Resource.Id.cmdP2CallMe);

        cmdOk = FindViewById<Button>(Resource.Id.cmdP2CallMe);
        cmdCancel = FindViewById<Button>(Resource.Id.cmdP2CallMe);

        // hide normal buttons
        // these all hide as expected
        cmdP2Update.Visibility = ViewStates.Gone;
        cmdP2Arrived.Visibility = ViewStates.Gone;
        cmdP2SelfDeployed.Visibility = ViewStates.Gone;
        // this should hide cmdCallMe but it doesn't change
        cmdP2CallMe.Visibility = ViewStates.Gone;
        // show confirm elements
        // this appears as expected
        lblConfirm.Visibility = ViewStates.Visible;
        // the following 2 buttons should appear, but don't
        cmdOk.Visibility = ViewStates.Visible;
        cmdCancel.Visibility = ViewStates.Visible;

AXML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:text="Status: EN-ROUTE"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lblP2Status"
        android:gravity="center" />
    <TextView
        android:text="00:00:00"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lblP2Timer"
        android:gravity="center"
        android:layout_marginBottom="19.0dp" />
    <Button
        android:text="Update"
        android:layout_width="match_parent"
        android:layout_height="64.5dp"
        android:id="@+id/cmdP2Update"
        android:layout_marginBottom="10.5dp"
        android:background="@android:color/holo_green_dark" />
    <Button
        android:text="ARRIVED"
        android:layout_width="match_parent"
        android:layout_height="64.5dp"
        android:id="@+id/cmdP2Arrived" />
    <Button
        android:text="SELF DEPLOYED"
        android:layout_width="match_parent"
        android:layout_height="64.5dp"
        android:id="@+id/cmdP2SelfDeployed"
        android:layout_marginBottom="10.5dp" />
    <Button
        android:text="CALL ME"
        android:layout_width="match_parent"
        android:layout_height="64.5dp"
        android:id="@+id/cmdP2CallMe"
        android:layout_marginTop="0.0dp" />
    <TextView
        android:text="Are you sure?"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lblP2Confirm"
        android:gravity="center"
        android:layout_marginBottom="16.0dp"
        android:visibility="gone" />
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1">
        <Button
            android:text="OK"
            android:layout_width="190dp"
            android:layout_height="wrap_content"
            android:id="@+id/cmdOk"
            android:visibility="gone" />
        <Button
            android:text="Cancel"
            android:layout_width="190dp"
            android:layout_height="wrap_content"
            android:id="@+id/cmdCancel"
            android:visibility="gone" />
    </LinearLayout>
</LinearLayout>

当我逐步完成时,所有值都按照我的预期分配。我不明白为什么元素可见性并不总是反射(reflect)这一点。

最佳答案

看看你的代码:

cmdP2CallMe = FindViewById<Button>(Resource.Id.cmdP2CallMe);

    cmdOk = FindViewById<Button>(Resource.Id.cmdP2CallMe);    <-- reference error here
    cmdCancel = FindViewById<Button>(Resource.Id.cmdP2CallMe);<-- reference error here

:)

关于c# - 有些 View 元素不会隐藏,有些不会出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47275907/

相关文章:

android - Android 中的短信文本计数器

android - 对话框 fragment 中的自动完成位置

android - 将 .NET Standard 类库转换为 native aar/jar

android - MvxCachingFragmentStatePagerAdapter 传递参数给viewmodel

c# - 对 TeamCity 上的程序集依赖链问题进行故障排除

c# - List<T> 线程安全

c# - 调用自己来绕过不同的线程? C#

c# - DatagridView Column.DataPropertyName 到数组元素?

android - 通知用户更新 Google Play 服务

xaml - Xamarin 形成具有 100% 父容器宽度和自动高度的图像以保持纵横比