2016-04-04 28 views
10

我正在使用由Google在我正在處理的項目中提供的PlaceAutocompleteFragment。但問題是我需要顯示我從自動填充小部件中選擇的地點名稱,但整個文本未顯示,因爲小部件中的默認文本大小過大。那麼,有什麼方法可以在不創建我自己的自定義搜索UI的情況下更改PlaceAutocompleteFragment中的文本大小?如何更改android中的地方autocompletefragment的文本大小?

我的XML代碼:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.baldysns.capedbaldy.materialdesigntest.activity.DrawerMainActivity" 
    tools:openDrawer="start"> 

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

     <LinearLayout 
      android:id="@+id/container_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/toolbar_drawer" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="?attr/colorPrimary" 
       android:minHeight="?attr/actionBarSize" 
       app:popupTheme="@style/AppTheme.PopupOverlay"> 

       <LinearLayout 
        android:id="@+id/lnr_google_searchbar" 
        android:layout_width="match_parent" 
        android:layout_height="35dp" 
        android:layout_marginTop="10dp" 
        android:layout_marginBottom="10dp" 
        android:background="@drawable/btn_white_notboerder"> 

        <fragment 
         android:id="@+id/place_autocompletehome_fragment" 
         android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" /> 
       </LinearLayout> 
      </android.support.v7.widget.Toolbar> 
     </LinearLayout> 

     <FrameLayout 
      android:id="@+id/container_body" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 

    </LinearLayout> 

    <fragment 
     android:id="@+id/fragment_navigation_drawer" 
     android:name="com.ibaax.com.ibaax.FragmentDrawer" 
     android:layout_width="300dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:layout="@layout/fragment_drawer" 
     tools:layout="@layout/fragment_drawer" /></android.support.v4.widget.DrawerLayout> 

和問題的截圖:

enter image description here

回答

27

使用片段比如,你可以更改字體大小和任何你想要的搜索框中EditText ...你這是怎麼需要做

// placeAutocompleteFragment - is my PlaceAutocompleteFragment instance 
((EditText)placeAutocompleteFragment.getView().findViewById(R.id.place_autocomplete_search_input)).setTextSize(10.0f); 

Happy_Coding;

+0

如果我的'placeAutocompleteFragment'處於'Linear Layout'內部,我需要將文本設置爲'placeAutocompleteFragment',因爲這不適用於此場景。有什麼建議麼。 –

+1

它失敗..java.lang.RuntimeException:無法啓動活動ComponentInfo java.lang.ClassCastException:android.widget.LinearLayout不能轉換爲android.widget.EditText –

+0

@dontknow可能你是使用不同的ID或從不同的片段視圖。請重新檢查。 – Bharatesh

0

你的屏幕截圖顯示了您在搜索圖標來設置保證金或填充和取消圖標。將其刪除,以便爲文本視圖提供更多空間。

您還可以使用autofittextviewhttps://github.com/grantland/android-autofittextview),它會根據你的文本視圖的大小使文字大小。

+0

但我使用的是由google提供的默認placeautocomplete小部件。我問有沒有什麼方法可以在小部件中改變這些東西。 –

2

最簡單的

((EditText)findViewById(R.id.place_autocomplete_search_input)).setTextSize(10.0f); 

skadosh答案的情況下,你需要先獲得片段的實例。

相關問題