2

我是安卓开发新手

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

自定义标题栏的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>

我的活动类中的代码

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

模拟器没有问题......它在那里工作正常,但如果有人告诉我解决方案,它不能在真实设备上工作。这是紧急的家伙......

4

2 回答 2

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

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

于 2013-10-04T07:43:02.687 回答
0

请从您的项目 res 文件夹中删除 values-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;
            }
        });
    }
}
于 2013-10-04T08:20:34.903 回答