XAMARIN:如何查找ViewById <tabwidget>? (找不到资源)

时间:2018-10-30 01:34:07

标签: android xamarin xamarin.android android-resources

当我尝试执行此操作时,出现资源未找到错误:

    TabWidget tabz = FindViewById<TabWidget>(Resource.Id.tabs);

即使在Main.axml文件中用id清楚地标记了TabWidget,编译器也看不到

错误CS0117'Resource.Id'不包含'标签'的定义

这是我的完整代码:

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
    android:id="@android:id/tabs"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="visible" />
<FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp" />
</LinearLayout>

编辑,对不起,这是完整的代码...我不知道为什么我上次没有复制此代码。上面的示例缺少TabHost

<?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">
    <TabHost
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minWidth="25px"
        android:minHeight="25px"
        android:id="@+id/tabHost1">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:id="@+id/linearLayout1">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    </TabHost>

</LinearLayout>

如何强制编译器识别我的标签ID?我试图重建resource.designer.cs ..将TabWidget的声明放入所有活动中……没有任何效果。我的C#看不到TabWidget

完整的项目可以在这里找到:

https://github.com/hexag0d/BitChute_Mobile_Android_a2

我也试过了,但是没有用 Xamarin Android Resource file not found

先谢谢

2 个答案:

答案 0 :(得分:0)

关于您评论中的问题:

1

  

这是一个很好的建议,但请注意,当   SetContentView(localLayout);用于已经存在的活动中   具有内容视图,它会使应用程序崩溃。

应用程序崩溃,因为SetContentView(localLayout)中的localLayout是LinearLayout,但是TabWidget需要TabHost。因此,您可以直接使用:

SetContentView(Resource.Layout.Main);

Main.xaml类似于:

<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:theme="@style/MyTheme">

   <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.0"
        android:visibility="visible" />
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp" />
    </LinearLayout>
</TabHost>

2

  

LinearLayout localLayout =   FindViewById(Resource.Layout.Main);视图   whichYouWantToUse_findViewById =   LayoutInflater.Inflate(Resource.Layout.Main,null); TextView电视=   FindViewById(Resource.Id.textView169); TabHost _tabHost =   (TabHost)YouWantToUse_findViewById.FindViewById(Resource.Id.tabhost);   '仍然看不到主持人……我在这里迷失了方向

之所以看不到tabhost,首先是TabHost是FrameLayout,而不是LinearLayout,第二,Resource.Id.tabhost应该是Android.Resource.Id.TabHost。所以我将您的代码编辑为:

    FrameLayout localLayout = FindViewById<FrameLayout>(Resource.Layout.Main);
    whichYouWantToUse_findViewById = LayoutInflater.Inflate(Resource.Layout.Main, null);
    //TextView tv = FindViewById<TextView>(Resource.Id.textView1);
    TabHost _tabHost = (TabHost)whichYouWantToUse_findViewById.FindViewById(Android.Resource.Id.TabHost);

3

  

所以有趣的是,如果我将tabhost更改为tahost ala   android:id =“ @ + id / tahost”,资源出现在设计器中,但是当   我更改为android:id =“ @ + id / tabhost”,该资源消失了。   奇怪..

TabHost的ID必须始终为@android:id / tabhost”,不能将其修改为tahost或其他任何主机。

所以onCreat方法中的代码如下:

    base.OnCreate(bundle);

    SetContentView(Resource.Layout.Main);
    View whichYouWantToUse_findViewById = LayoutInflater.Inflate(Resource.Layout.layout1, null);
    TextView textInAnotherLayout = (TextView)whichYouWantToUse_findViewById.FindViewById(Resource.Id.textView1);
    textInAnotherLayout.Text = "Yeah";

    TabHost tabHost = FindViewById<TabHost>(Android.Resource.Id.TabHost);
    TabWidget tabWidget = tabHost.TabWidget;
    var tabContent = tabHost.TabContentView;

    FrameLayout localLayout = FindViewById<FrameLayout>(Resource.Layout.Main);
    whichYouWantToUse_findViewById = LayoutInflater.Inflate(Resource.Layout.Main, null);
    TabHost _tabHost = (TabHost)whichYouWantToUse_findViewById.FindViewById(Android.Resource.Id.TabHost);

以下是旧答案。


  

已修改答案:在活动中获取TabHost和TabWidget的方法:

TabHost tabHost = this.TabHost;
TabWidget tabWidget = tabHost.TabWidget;
     

在活动中使用一个XAML文件中的一种资源的方式   另一个:

    LinearLayout localLayout = FindViewById<LinearLayout>(Resource.Layout.Main);
    SetContentView(localLayout);
    View whichYouWantToUse_findViewById = LayoutInflater.Inflate(Resource.Layout.layout1, null);
    Button button1 = (Button)whichYouWantToUse_findViewById.FindViewById(Resource.Id.button1);
     
    

我下载了您的项目并通过修改> android:id

的属性解决了此问题          

                       

  

答案 1 :(得分:0)

问题不在于Visual Studio编译器,而在于您为View声明ID的方式,

如果您查看James White的this博客,您将了解IdLayout属性在Android中的实际工作方式,

正确的声明ID的方式应该是这样的android:id="@+id/tabs",而您正在做的事情就是这个android:id="@android:id/tabs"

因此,当您添加它们并尝试使用Resource.Id.tabs查找它们时,由于Xamarin ResourceDesginer.cs文件从未添加过,因此名称标签中的ID中没有可用的内容。

所以最终结果实际上应该是这样的:

  <TabHost
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ffc916"
                android:orientation="vertical">
        </LinearLayout>
        </FrameLayout>
    </LinearLayout>
  </TabHost>

另外,请注意,我已经用fill_parent替换了所有match_parent,这是因为填充父级已过时,或者您可以说替换(在API-8之后),因此,不得使用here

更新

我更新了选项卡宿主的工作方式,还更新了上面的XML,并添加了reference link,在这里您可以找到同时使用TabHost和TabWidget的正确方法,也是不错的SO同样的例子。