2016-12-20 29 views
1

我想添加进度条来搜索视图。但searchPlate返回null。Xamarin.Android如何添加进度条到搜索视图

public void showProgressBar(SearchView searchView, Activity context) 
{ 
    var id = searchView.Context.Resources.GetIdentifier("android:id/search_plate", null, null); 
    var searchPlate = searchView.FindViewById(id); 

    ... 
} 

请帮我

更新
我在菜单中创建搜索视图

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <item android:id="@+id/action_item_search" 
     android:title="Seach Item" 
     android:icon="@drawable/ic_search_black_24dp" 
     app:showAsAction="always|collapseActionView" 
     app:actionViewClass="android.support.v7.widget.SearchView" /> 
</menu> 

回答

0

机器人:ID/search_plate是指在搜索查看一些背景的作品。你不能用进度条替换它。 SearchView扩展了LinearLayout,因此可以使用SearchView完成任何可以使用LinearLayout完成的任务。但是,SearchView是一种特殊用途的视图。我不会建议修改其默认行为。因此,我建议将ProgressBar添加到SearchView的右侧,并将ProgressBar放入不确定模式,这是一些旋转轮。

+0

我尝试实施http://stackoverflow.com/a/26143787/1268455上xamarin但searchPlate = NULL。 –

0

我可以用你的代码让我身边的searchPlate:

public class MainActivity : Activity 
{ 
     SearchView sv; 
     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 
      // Set our view from the "main" layout resource 
      SetContentView (Resource.Layout.Main); 
      sv = FindViewById<SearchView>(Resource.Id.searchView1); 
      showProgressBar(sv, this); 
     } 

     void showProgressBar(SearchView searchView, Context context) 
     { 
      int id = searchView.Context.Resources.GetIdentifier("android:id/search_plate", null, null); 
      View searchPlate = searchView.FindViewById(id); 

      if (searchView.FindViewById(id).FindViewById(Resource.Id.progressBar1) != null) 
      { 
       searchView.FindViewById(id).FindViewById(Resource.Id.progressBar1).Animate().SetDuration(200).Alpha(1).Start(); 
      } 
      else 
      { 
       View v = LayoutInflater.From(context).Inflate(Resource.Layout.loading, null); 
       ((ViewGroup)searchView.FindViewById(id)).AddView(v, 1); 
      } 
     } 
    } 

你可以参考我的项目,找出核心问题。

Here is the project address

enter image description here

+0

感谢您的演示。但我使用菜单创建搜索视图。 –

+0

@TuanHoangAnh你说你有'search_plate'为null,在那之前你是如何从菜单中获取searchView对象的? –