Android自定义样式和标题完全被忽略或出错

时间:2013-06-08 12:13:44

标签: android

我无法获得自定义标题文字或自定义标题样式。我必须做一些非常错误的事情,但我无法弄清楚是什么(以及两个问题是否相关)

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);
  }                  
}

1 个答案:

答案 0 :(得分:0)

尝试以下方式。

onCreate()

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

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 /值/ 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 /抽拉/ 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>

我希望这会对你有所帮助。