1

在构建应用程序时,有时我们需要创建自定义窗口标题以适应我们的需求,并使我们的应用程序与其他应用程序不同。我在通过此代码在首选项活动上创建图标窗口标题时遇到问题:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_LEFT_ICON);
        addPreferencesFromResource(R.layout.setting);
        setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.setting);
    }

有谁能够帮我?

4

4 回答 4

2
public class Preferences extends PreferenceActivity {  
protected ImageView img;
   @Override
   protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);        
    setContentView(R.layout.main_tab);
    title = (TextView) findViewById(R.id.img);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
    Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.sc01);
    int width=30;
    int height=20;
    Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
    this.img.setImageBitmap(resizedbitmap);

  }
}
///////In title_bar.xml///////

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout01"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
     >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="3dp">
    </ImageView>



</RelativeLayout>
于 2012-08-10T00:46:12.473 回答
0

您可以在清单文件中更改偏好活动的图标和标题:

    <activity
        android:name="PATH TO YOUR ACTIVITY"
        android:label="@string/title_from_resources" 
        android:icon="@drawable/icon_from_resources" >
    </activity>
于 2013-08-05T13:52:57.677 回答
0

尝试:

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.drawable.icon);
于 2012-08-10T00:25:05.910 回答
0

尝试这个

  1. 在“layout”文件夹中为窗口标题创建自定义布局。

window_title.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="35dip"
    android:gravity="center_vertical"
    android:paddingLeft="5dip"
    android:background="#323331">

    <ImageView
        android:id="@+id/header"
        android:src="@drawable/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>
  1. 在“values”文件夹中创建自定义样式。

custom_style.xml

<resources>
    <style name="CustomWindowTitleBackground">
        <item name="android:background">#323331</item>
    </style>

    <style name="CustomTheme" parent="android:Theme">
        <item name="android:windowTitleSize">35dip</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>
</resources>
  1. 在清单文件中应用自定义样式作为主题。

AndroidManifest.xml

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

4.在主活动类中应用自定义窗口标题

自定义窗口标题

    public class CustomWindowTitle extends PreferenceActivity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,  R.layout.Your_Custom_Title);
super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.YourPrf);
        }
    }
于 2012-08-10T01:46:00.613 回答