0

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

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

1 回答 1

0

试试下面的方法。

在你的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>

我希望这能帮到您。

于 2013-06-08T12:35:56.387 回答