2016-06-10 24 views
0

我想在我的应用程序中使用谷歌地方选择器。我跟着这个链接 https://developers.google.com/places/android-api/placepicker 并在我的片段中添加了位置选取器,但它没有得到我的应用程序的颜色或主题。位置选取器的工具栏显示为白色。我怎样才能让谷歌地方选择器看起来像我的应用程序的主题?

它在文档中写道,该选择器从应用程序的材质主题中获取colorPrimary。我也有colorPimarycolorPrimaryDark我的style.xml

这里怎么回事?

代码:

public class NameOfBusinessFragment extends Fragment { 

    int PLACE_PICKER_REQUEST = 1; 
    int RESULT_OK = -1; 
    PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder(); 
    EditText category,location; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 


     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View view = inflater.inflate(R.layout.fragment_name_of_business, 
        container, false); 

      location = (EditText)view.findViewById(R.id.location); 

      location.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        try { 

         startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST); 
        } 
        catch (GooglePlayServicesRepairableException e) 
        { 
         Toast.makeText(getActivity(),"ServiceRepaire Exception",Toast.LENGTH_SHORT).show(); 
        } 
        catch (GooglePlayServicesNotAvailableException e) 
        { 
         Toast.makeText(getActivity(),"SeerviceNotAvailable Exception",Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }); 

      return view; 
     } 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == PLACE_PICKER_REQUEST) { 
      if (resultCode == RESULT_OK) { 
       Place place = PlacePicker.getPlace(data, getActivity()); 
       String toastMsg = String.format("Place: %s", place.getName()); 
       Toast.makeText(getActivity(), toastMsg, Toast.LENGTH_LONG).show(); 
       location.setText(place.getName()); 
      } 
     } 
    } 
} 

Style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
    <item name="windowActionBarOverlay">false</item> 
    <item name="windowActionBar">false</item> 
    <item name="android:windowBackground">@android:color/white</item> 
    <item name="android:windowContentOverlay">@null</item> 

    <item name="android:editTextStyle">@style/EditTextStyle</item> 

</style> 

<style name="EditTextStyle" parent="Widget.AppCompat.EditText"> 
    <item name="colorControlNormal">@color/colorAccent</item> 
    <item name="colorControlActivated">@color/colorAccent</item> 
    <item name="colorControlHighlight">@color/colorAccent</item> 
</style> 

<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 

如何获得呢?

+0

你有这个问题的解决方案?显然我在4.2.2也面临同样的问题。设备。它似乎在5.1.1设备上正常工作。 –

回答

0

尝试更换

startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST); 

Intent placePickerIntent = builder.build(getActivity()); 
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { 
    placePickerIntent.putExtra("primary_color", ContextCompat.getColor(this, R.color.colorPrimary)); 
    placePickerIntent.putExtra("primary_color_dark", ContextCompat.getColor(this, R.color.colorPrimaryDark)); 
} 
startActivityForResult(placePickerIntent, PLACE_PICKER_REQUEST); 

地点选择器不会自动地挑选颜色从主题。这是一个如here所讨论的错误。还要注意,这是一个破解,而不是一个实际的干净解决方案。

+0

我尝试着只为其他应用程序启动一个意图,并且它在同一设备上自动选择主题颜色。不知道其他应用程序有什么问题,我对这两个应用程序都具有相同的样式。 @Obscure极客 – Sid

+0

有趣。同样的风格和相同的设备。设计和app-compat库版本是否也相同? –

+0

是的,我刚刚更改了应用的软件包名称,并根据需要进一步更改了活动和设计。当我试图实现地方选择器与以前的应用程序相同,它的工作。仍然不适用于以前的应用程序。 @Obscure Geek – Sid

相关问题