2015-11-15 37 views
0

我有一個活動,承載FrameLayout並動態顯示自定義片段。爲什麼Android主題不影響片段內的元素?

我的目標是在styles.xml中定義適用於所有片段內所有按鈕的按鈕樣式。

這是我styles.xml文件:

<!-- Base application theme. --> 
<style name="AppThemeDark" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="android:buttonStyle">@style/bt</item> 
</style> 

<style name="bt" parent="@android:style/Widget.Button"> 
    <item name="android:background">#992323</item> 
</style> 

activity_login.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.app.LoginActivity"> 


    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/login_frame_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"/> 

</LinearLayout> 

終於fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     tools:context="com.example.app.LoginBackupKeyFragment" 
     android:orientation="vertical"> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Got it!" 
     android:id="@+id/button" 
     android:layout_gravity="center_horizontal" 
     android:onClick="BackupKeyGotItButtonClicked"/> 


</LinearLayout> 

但是風格不適用!爲什麼?

編輯二零一五年十一月一十五日

閱讀這個問題後,我想我應該提到,我使用自定義功能onCreateView在我的片段,它看起來像這樣:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    // create ContextThemeWrapper from the original Activity Context with the custom theme 
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.AppThemeDark); 

    // clone the inflater using the ContextThemeWrapper 
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper); 

    View fragmentView = localInflater.inflate(R.layout.fragment_login_new, container, false); 

    pass1 = (EditText)fragmentView.findViewById(R.id.login_new_pass1); 

    return fragmentView; 
} 

EDIT 2 15.11 .2015 這是我的清單XML

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.app" > 

    <uses-permission android:name="android.permission.READ_SMS" /> 

    <application> 

     <activity android:name=".StartUpActivity" 
      android:theme="@style/AppTheme.NoActionBar" 
      android:noHistory="true"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 

       <action android:name="android.intent.action.DEFAULT" /> 
      </intent-filter> 
     </activity> 


     <activity 
      android:name=".LoginActivity" 
      android:label="@string/title_activity_login" 
      android:theme="@style/AppThemeDark" 
      android:windowSoftInputMode="adjustResize"> 
     </activity> 
    </application> 

</manifest> 

編輯3 !!重要!

我已經注意到這必須與

<item name="android:buttonStyle">@style/bt</item> 

如例如有關加入這

<item name="colorControlNormal">#f00</item> 
<item name="colorControlActivated">#0f0</item> 
<item name="colorControlHighlight">#00f</item> 

AppThemeDark風格的作品和顯示在片段確定。僅僅是android:buttonStyle並且相似地例如android.editTextStyle不起作用。

+0

請發表您的manifest.xml代碼 –

+0

我已經發布清單 –

回答

0

可能爲時已晚,但如果我理解正確的話,你要設置不同的colorControlNormalcolorControlActivatedcolorControlHighlight比你的主題全局指定並應用它只是一個按鈕。我有(我想白強調顏色EditText)類似的問題,我已經解決它像如下:

  1. values/styles創建樣式:

    <style name="ColorOverwrite" parent="AppTheme"> <item name="android:textColorHint">@color/white_54p</item> <item name="colorAccent">@color/colorWhite</item> <item name="colorControlNormal">@color/white_54p</item> <item name="colorControlActivated">@color/colorWhite</item> </style>

  2. 添加app:theme="@style/ColorOverwrite"UPDATEapp:theme="..."現已棄用,我們應該使用android:theme="")進行某些控制(在我的情況下爲EditText)

    <EditText android:id="@+id/id" android:textColor="@color/colorBlueLight" android:inputType="text" app:theme="@style/ColorOverwrite" android:layout_width="match_parent" android:layout_height="wrap_content" />

  3. 確保您該控件不組在同一時間style="@style/somestyle",否則你會得到一個錯誤。

Example image showing applied theme - EditText紅色矩形的我的自定義主題,EditText黃色矩形顯示了主題

默認的色彩風格
相關問題