Android中自定义标题栏(二)

来源:互联网 发布:app软件破解器 编辑:程序博客网 时间:2024/05/17 22:14

activity代码

package com.android.demo;import android.app.Activity;import android.os.Bundle;import android.view.Window;import android.widget.ImageView;import android.widget.TextView;public class MytestActivity extends Activity {private TextView myText;private ImageView myImage;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);          setContentView(R.layout.main);          getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar);                    myText = (TextView)findViewById(R.id.myText);          myText.setText("自定义标题栏");        myImage = (ImageView)findViewById(R.id.myImage);        myImage.setBackgroundResource(R.drawable.ic_launcher);    }}
titlebar.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="horizontal"     ><ImageView     android:id="@+id/myImage"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    /><TextView    android:id="@+id/myText"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:gravity="center"     /></LinearLayout>

效果图:


原创粉丝点击