【问题标题】:Forcing app using ActionBarSherlock to use Theme and not Theme.Holo强制使用 ActionBarSherlock 的应用程序使用 Theme 而不是 Theme.Holo
【发布时间】:2012-11-14 10:31:24
【问题描述】:

我有一个最初为 Android 2.3 设计的应用。现在我正在添加一个带有 ActionBarSherlock 的 ActionBar,一切都很好,除了在运行 4.2 的 Nexus 7 上,它现在将 Holo 主题用于按钮和 EditTexts。之前我的主题是从 Theme.Light 继承的,现在它是从 Theme.Sherlock 继承的。我希望它即使在较新的设备上也能使用旧主题。我怀疑这是由 values-v14 文件夹中的以下行引起的:

<style name="Sherlock.__Theme" parent="android:Theme.Holo">

所以我尝试删除该文件夹和 v11 文件夹只是为了看看它是否能解决问题,但后来 ActionBar 完全消失了。

然后我尝试像这样覆盖 EditText 的样式:

<style name="AppTheme" parent="Theme.Sherlock">
    <item name="@android:attr/editTextStyle">@android:style/Widget.EditText</item>
</style>

这应该使小部件不使用 Widget.Holo 主题并使用常规主题,但它不起作用。

如何让 Android 4.0+ 设备在使用 ABS 时使用原始主题而不是 Holo 主题?

下面我发布了一些使用 ABS 的示例应用程序的代码,该应用程序当前显示的是 Holo 主题的 EditText,但应该显示常规的 EditText:

MainActivity.java:

import android.os.Bundle;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;

public class MainActivity extends SherlockActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

activity_main.xml:

<RelativeLayout 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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
    <EditText 
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

</RelativeLayout>

styles.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppTheme" parent="Theme.Sherlock">
        <item name="@android:attr/editTextStyle">@android:style/Widget.EditText</item>
    </style>

</resources>

另外,manifest 中的主题是 AppTheme,ABS 被添加为库项目。

【问题讨论】:

    标签: android actionbarsherlock android-4.0-ice-cream-sandwich android-theme


    【解决方案1】:

    这不会是由 ActionBarSherlock 引起的,EditTextButton 主题是由您运行的平台定义的,在本例中为 4.2。 EditTextsButtons 的外观也可能因 UI 皮肤(TouchWiz、Sense、MotoBlur 等)而有所不同。

    我建议将 Holo 主题保留在支持它的设备上,因为用户会熟悉它并且在 3.x+ 上使用 2.x 主题会显得格格不入。

    此外,需要Sherlock 主题之一才能使用ActionBarSherlock according to the docs

    为了使自定义操作栏实现正常运行,您的应用程序必须使用 Theme.Sherlock、Theme.Sherlock.Light 或 Theme.Sherlock.Light.DarkActionBar,或者您的自定义主题必须使用上述其中之一作为其父主题。

    如果您真的想要 2.x 查找 Buttons 和其他 Widgets,我相信您需要从 2.x 复制所有样式/可绘制对象,然后在 Sherlock 派生主题之一中覆盖它们。你可以看看 Chris Versieux 对 HoloEverywhere 所做的事情,然后尝试反过来让一切看起来像 2.x,不过我强烈建议不要这样做。

    【讨论】:

      【解决方案2】:

      我的修复不起作用的原因是 Widget.EditText 的背景属性使用 ?attr 语法,这意味着它使用的是当前主题的背景,即 Holo。因此,我基本上查看了 Android 源代码,并将使用此语法的两个属性改回了我认为应该是 Holo 之前的主题样式(即将背景设置为白色)。

      【讨论】:

      • 我也遇到了同样的问题,请问你是怎么解决的。
      猜你喜欢
      • 1970-01-01
      • 2018-05-22
      • 2014-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 2017-10-07
      • 2017-03-12
      相关资源
      最近更新 更多