【问题标题】:Android custom styles and titles completely ignored or gives errorAndroid 自定义样式和标题完全被忽略或给出错误
【发布时间】:2013-06-04 15:58:06
【问题描述】:

我无法让自定义标题文本或自定义标题样式正常工作。我一定是做错了什么,但我不知道是什么(以及这两个问题是否相关)

1) 关于样式(似乎被忽略)我已经在 valyes/styles.xml

中尝试过
   <style name="MicWindowTitleBackground">
        <item name="android:background">@android:color/black</item>
        <item name="android:padding">0px</item>
    </style>

   <style name="MicWindowTitleTextAppearance">
       <item name="android:textSize">20dip</item>
       <item name="android:textStyle">bold|italic</item>        
   </style>       

   <style name="MicWindowTitle">
     <item name="android:textAppearance">@style/MicWindowTitleTextAppearance</item>
   </style>

   <style name="MicTheme" parent="AppTheme">
     <item name="android:windowTitleSize">44dip</item>
     <item name="android:windowTitleBackgroundStyle">@style/MicWindowTitleBackground</item>
     <item name="android:windowTitleStyle">@style/MicWindowTitle</item>     
   </style>

这在 AndroidManifest.xml

  <application
    android:allowBackup="true"
    android:icon="@drawable/replace__logo__app_android"
    android:label="@string/MicAppName"
    android:theme="@style/MicTheme"
    android:name="com.example.example.MicApp"    
  >

2) 或者,我想我可以使用 FEATURE_CUSTOM_TITLE,这会带来额外的好处,我可以更改标题栏中显示的标题。但是,此代码错误并带有错误消息,我*无法将 FEATURE_CUSTOM_TITLE 与其他标题功能结合*(据我所知...)

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String customTitleStr = getIntent().getExtras().getString("Title");

Boolean customTitleSupported = this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

if ( (customTitleSupported) && (customTitleStr != null) ) {
  getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.override_titlebar);
  final TextView myTitleText = (TextView) findViewById(R.id.myTitleText);
  if ( myTitleText != null ) {
      myTitleText.setText(customTitleStr);
  }                  
}

【问题讨论】:

    标签: android


    【解决方案1】:

    试试下面的方法。

    在你的onCreate()

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.activity_main);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                    R.layout.titlebar);
    

    标题栏.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:padding="1dp" >
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/logo" />
    
        <TextView
            android:id="@+id/left_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"        
            android:textColor="#fff"
            android:paddingLeft="2dp"        
            android:text="@string/app_name" />
    
    </LinearLayout>
    

    清单文件

    <application
            android:icon="@drawable/logo"
            android:label="@string/app_name"
            android:theme="@style/CustomTheme" >
            .....
    </application>
    

    res/values/custom_title.xml

    <resources>
        <style name="CustomWindowTitleBackground">
            <item name="android:background">@drawable/title_shape</item>
        </style>
    
        <style name="CustomTheme" parent="android:Theme">
            <item name="android:windowTitleSize">33dip</item>
            <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
        </style>
    </resources>
    

    res/drawable/title_shape.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
    
        <stroke
            android:width="1dp"
            android:color="#919090" />
    
        <gradient
            android:angle="225"
            android:endColor="#DD000000"
            android:startColor="#02085E" />
    
    </shape>
    

    希望对你有所帮助。

    【讨论】:

    • 我收到关于 FEATURE_CUSTOM_TITLE 的相同错误。这可能是目标 SDK 问题或类似问题吗?或者我收到错误后在 AndroidManifest.xml 中设置了什么?
    • 这将完美运行。是否在清单文件中将android:theme="@style/CustomTheme" 设置为application 的子标签?另请参阅here
    • 我一定是个盲人。您设置“android:theme”的方式与我在原始示例中使用的完全相同方法不同?
    • 我将把它标记为有效答案。 (虽然我无法让它工作)将这两个问题合二为一是一个错误,因为我没有任何记录者相信它们是相关的。一个问题是关于标题的主题化,另一个是关于 feature_custom_title 的问题。我将把它创建为单独的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 2012-11-19
    相关资源
    最近更新 更多