我有一個活動,承載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
不起作用。
請發表您的manifest.xml代碼 –
我已經發布清單 –