如何在首选项活动android上创建自定义窗口标题?

时间:2012-08-10 00:23:09

标签: android icons title preferenceactivity preference

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

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 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

尝试:

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.drawable.icon);

答案 2 :(得分:0)

试试这个

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

    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”文件夹中创建自定义样式。
    2.   

      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. 将清单文件中的自定义样式应用为主题。
      2.   

        的AndroidManifest.xml

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

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

          

        CustomWindowTitle

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

答案 3 :(得分:0)

您可以在清单文件中更改首选项活动的图标和标题:

    <activity
        android:name="PATH TO YOUR ACTIVITY"
        android:label="@string/title_from_resources" 
        android:icon="@drawable/icon_from_resources" >
    </activity>