Android 自定义全局标题栏 解决输入法遮住屏幕问题

自定义标题栏有两个方式

1.把系统标题栏隐藏 显示自己做的xml 这种方法有个bug是在输入法弹出的时候会覆盖应用的界面 不是我们要的效果

2.自定义系统的标题栏 这种应用如果需要输入法输入法弹出后,系统可以自己调整适应屏幕而不会遮住屏幕

做法如下

 <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            android:windowSoftInputMode="adjustResize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


styles.xml

<resources>

    <style name="CustomWindowTitleBackground">
        <item name="android:background">@drawable/bg_btn_nor</item>
    </style>

    <style name="AppTheme" parent="android:Theme.Light">
        <item name="android:gravity">center</item>
        <item name="android:textSize">20dip</item>
        <item name="android:windowTitleSize">50dp</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>

</resources>

在activity中这样写

public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_main);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);// 设置自定义标题栏的另一种方法
		context = this;

	}

这是自定义的titile

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title_panel"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:padding="1dip" >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="分享"
        android:textSize="20dip" 
        android:textColor="#FFFFFF"/>

    <LinearLayout
        android:id="@+id/titlelin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:gravity="center" >

        <CheckBox
            android:id="@+id/checkBoxqq"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/checkbox_style_qq" />

        <CheckBox
            android:id="@+id/checkBoxrenren"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/checkbox_style_renren" />

        <CheckBox
            android:id="@+id/checkBoxsina"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/checkbox_style_sina" >
        </CheckBox>
    </LinearLayout>

</RelativeLayout>


posted @ 2012-11-21 19:14  sfshine  阅读(840)  评论(0编辑  收藏  举报