2014-12-26 76 views
6

我做材料的設計風格應用造型AutoCompleteTextView與AppCompat.EditText不起作用

我想
改變AutoCompleteTextView風格,以風格像android.support.v7.internal.widget.TintEditText

我加的風格我style.xml:

<style name="AppTheme" parent="AppTheme.Base"/> 
<style name="AppTheme.Base" parent="Theme.AppCompat"> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="android:autoCompleteTextViewStyle">@style/AutoCompleteTextViewAppTheme</item> 
</style> 
<style name="AutoCompleteTextViewAppTheme" parent="Base.Widget.AppCompat.EditText"/> 

雖然有效,但線條顏色不變。 enter image description here

帶材料設計的EditText似乎使用colorControlActivatedcolorControlNormal。因此,我試圖在先前的樣式定義中覆蓋這些屬性,但它沒有效果。
我需要做什麼,使其工作?

回答

5

嘗試添加所需的樣式在XML中使用的窗口小部件是這樣的:

<View 
style="@style/NameOfYourTheme" 
... 
/> 

如果不工作,你可以嘗試自己的風格吧。 您將不得不更改TextView屬性,您可以查看here

TEXTCOLOR一個實例可以通過改變android:textColor屬性,它應該被添加到你的風格,例如改變:

<style name="AutoCompleteTextViewAppTheme" parent="Base.Widget.AppCompat.EditText"/> 

<item name="android:textColor">#ffffffff</item> 
</style> 

如果你想改變你必須改變的背景屬性的EditText線,例如以這種方式:

<item name ="android:background="@drawable/line_background"> </item> 

,並添加新的文件line_background.xml在繪圖資源文件夾類似的內容:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="line"> 


    <stroke 
     android:color="#c0c000" 
     android:width="3dp"></stroke> 
</shape> 
+0

文字顏色屬性適用處理popupBackground屬性的類,但問題是,我不能改變線條顏色(它在屏幕上是黑色的) – CodeNinja

+0

看我最近的編輯 –

+2

這只是通過中間畫一條線。它不會執行材質下劃線。 – radley

3

我已經創建了定製的Tint Aware AutoCompleteTextView來解決這個問題。

package com.atrinax.gist; 

import android.content.Context; 
import android.support.v7.internal.widget.TintTypedArray; 
import android.util.AttributeSet; 
import android.widget.AutoCompleteTextView; 

public class TintAutoComplete extends AutoCompleteTextView { 

private static final int[] TINT_ATTRS = { 
     android.R.attr.background 
}; 

public TintAutoComplete(Context context) { 
    this(context, null); 
} 

public TintAutoComplete(Context context, AttributeSet attrs) { 
    this(context, attrs, android.R.attr.editTextStyle); 
} 

public TintAutoComplete(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 

    TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs, TINT_ATTRS, 
      defStyleAttr, 0); 
    setBackgroundDrawable(a.getDrawable(0)); 
    a.recycle(); 
} 
} 

將此課程添加到您的項目中。 使用在這樣的XML:

<com.atrinax.gist.TintAutoComplete 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" /> 

下行這種解決方案之一是,它依賴於內部類

android.support.v7.internal.widget.TintTypedArray 

可能改變或在未來成爲不可用。 請看下面Derek的回答 - 它正在解決一個由我的答案引發的背景問題。

+0

這不包括v21上的下拉背景這變得透明。從我的測試4.4和以下默認爲黑暗的背景。 – Derek

+0

我知道這個問題,但我似乎無法找到一個解決方案,其他設置您自己的佈局列表項或通過更改適配器中的視圖,例如:if(convertView == null){ convertView = getLayoutInflater()。inflate(android.R.layout.simple_list_item_1,null); convertView.findViewById(android.R.id.text1).setBackgroundColor(backgroundLightColor); }' –

+1

我找到了解決方案,請參閱我的答案獲取更多信息。感謝分享這段代碼,非常酷的方法。 – Derek

2

來推斷上@ Atrinax的答案,這裏是一個將被延長AutoCompleteTextView並將其添加到TINT_ATTRS

public class TintAutoCompleteTextView extends AutoCompleteTextView { 

private static final int[] TINT_ATTRS = { 
     android.R.attr.background, 
     android.R.attr.popupBackground 
}; 

public TintAutoCompleteTextView(Context context) { 
    this(context, null); 
} 

public TintAutoCompleteTextView(Context context, AttributeSet attrs) { 
    this(context, attrs, android.R.attr.autoCompleteTextViewStyle); 
} 

public TintAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 

    TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs, TINT_ATTRS, 
      defStyleAttr, 0); 
    setBackgroundDrawable(a.getDrawable(0)); 
    setDropDownBackgroundDrawable(a.getDrawable(1)); 
    a.recycle(); 
} 
}