2015-11-15 38 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黄色矩形显示了主题

默认的色彩风格
相关问题