自定义标题栏功能在模拟器中有效,但在设备中不起作用

时间:2013-10-04 07:23:26

标签: android custom-titlebar

我是Android开发的新手

我尝试在我的应用程序中创建自定义标题栏。它在模拟器中工作。但是当我在我的设备中运行这个应用程序时...它停在那里(没有加载)

自定义标题栏的xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/left_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:background="@drawable/bg2"
    android:text="Welcome Screen"
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:textColorHighlight="#ffffd4"
    android:textAllCaps="false"
    android:typeface="serif"
    android:textColor="#960000"
    android:textStyle="italic" />

</RelativeLayout>
我的Activity类中的

代码

public void onCreate(Bundle b){
    super.onCreate(b);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.welcome);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitlebar);

在模拟器中没有问题......它在那里工作正常,但在真实设备中不起作用 如果任何一个plzz告诉我解决方案。这是紧急的人......

2 个答案:

答案 0 :(得分:0)

onCreate()方法中尝试以下操作:

super.onCreate(savedInstanceState);
final boolean customTitleSupported = 
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if (customTitleSupported) {
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
        R.layout.titlebar);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if (myTitleText != null) {
    myTitleText.setText("CUSTOM TITLE");
}

首先检查是否支持自定义标题。

答案 1 :(得分:0)

请从项目资源文件夹中删除值-v11和values-v14文件夹,并尝试以下代码

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author android
 * 
 */
public class MainActivity extends Activity {

    /**
     * 
     */
    private TextView textView;

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_CUSTOM_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.custom_title_layout);
        textView = (TextView) getWindow().findViewById(R.id.titleText);
        System.out.println("Text View is : " + textView);
        textView.setText("My Custom Title");
        ImageView titleImageView = (ImageView) getWindow().findViewById(
                R.id.customTitleImage);
        titleImageView.setOnTouchListener(new OnTouchListener() {
            /*
             * (non-Javadoc)
             * 
             * @see android.view.View.OnTouchListener#onTouch(android.view.View,
             * android.view.MotionEvent)
             */
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Toast.makeText(MainActivity.this,
                        "Title Image On Touch Event Occured.",
                        Toast.LENGTH_LONG).show();
                return true;
            }
        });
    }
}