2016-06-10 41 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

相關問題