一、先上效果图
二、涉及的文件
三、自定义标题栏
1、主要思路
一句话:把原来的Title干掉:<item name="android:windowNoTitle">true</item>(在自定义标题样式中,位于styles.xml中),然后在activity_main.xml中自己定义一个。
2、操作步骤
(1)在styles.xml中自定义样式(<style name="XTheme" parent="android:Theme">),注意把原来的title干掉(<item name="android:windowNoTitle">true</item>)
(2)在AndroidMainfest.xml文件中添加activity样式(android:theme="@style/XTheme")
(3)在activity_main.xml自己定义一个标题栏
四、popupWindow及其动画效果
1、简要说明一下
Mypopwindow.xml——popupWindow的自定义布局
MainActivity.java——定义popupWindow显示方法并调用
styles.xml——写了动画效果的style
fade_in.xml,fade_out.xml——定义进出动画的效果
五、还是上代码吧(按照截图,从上到下)
1、MainActivity.java
<span style="font-size:14px;">package com.example.my_title1;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageButton imageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showPopWindow(MainActivity.this, v);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void showPopWindow(Context context, View parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View vPopWindow = inflater.inflate(R.layout.mypopwindow, null,
false);
// 宽300 高300
final PopupWindow popWindow = new PopupWindow(vPopWindow, 300, 500,
true);
Bitmap bitmap = null;
popWindow.setBackgroundDrawable(new BitmapDrawable(getResources(),
bitmap));
popWindow.setAnimationStyle(R.style.AnimationPreview); // 设置动画效果
Button okButton = (Button) vPopWindow.findViewById(R.id.button1);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "You click OK",
Toast.LENGTH_SHORT).show();
}
});
Button cancleButton = (Button) vPopWindow.findViewById(R.id.button2);
cancleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popWindow.dismiss(); // Close the Pop Window
}
});
popWindow.showAtLocation(parent, Gravity.LEFT | Gravity.CENTER, 0, 0);
}
}</span><span style="font-size:18px;">
</span>
2、fade_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="700"
android:fillAfter="false"
android:fromXScale="0.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="0%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
3、fade_out.xml
<span style="font-size:14px;"><set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="100"
android:fillAfter="false"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.0"
android:toYScale="0.0" />
</set></span>
4、activity_main.xml
<span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="192dp"
android:text="Button"
tools:ignore="HardcodedText" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#008000" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#008000"
android:text="自定义标题栏"
android:textColor="#FFFFFF"
tools:ignore="HardcodedText" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/login_button_selector"
android:src="@drawable/ic_action_user"
tools:ignore="ContentDescription" />
</RelativeLayout>
</RelativeLayout></span>
5、mypopwindow.xml
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#008000"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#008000"
android:text="这是popwindow"
android:textSize="20sp"
android:textColor="#FFFFFF"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="哈哈哈哈点击吧"
android:textSize="20sp"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="哈哈哈哈取消吧"
android:textSize="20sp"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="哈哈哈哈点击吧"
android:textSize="20sp"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="哈哈哈哈点击吧"
android:textSize="20sp"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="哈哈哈哈点击吧"
android:textSize="20sp"
tools:ignore="HardcodedText" />
</LinearLayout></span>
6、strings.xml
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MY_title1</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<drawable name="clr_normal">#FF008000</drawable>
<drawable name="clr_pressed">#FF666666</drawable>
</resources></span>
7、styles.xml
<span style="font-size:14px;"><resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<!-- 自定义标题样式 -->
<style name="StatusBarBackground">
<item name="android:background">#008000</item>
</style>
<style name="XTheme" parent="android:Theme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowTitleBackgroundStyle">@style/StatusBarBackground </item>
<item name="android:background">@android:color/white</item>
<item name="android:textSize">23sp</item>
<item name="android:windowTitleSize">50dip</item>
</style>
<!-- 自定义标题样式 -->
<!-- popupwindow_demo 中动画文件 -->
<style name="AnimationPreview">
<item name="android:windowEnterAnimation">@anim/fade_in</item>
<item name="android:windowExitAnimation">@anim/fade_out</item>
</style>
</resources></span>
8、AndroidMainfest.xml
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.my_title1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18"
tools:ignore="OldTargetApi" />
<application
android:allowBackup="true"
android:icon="@drawable/aaa"
android:label="自定义标题栏" >
<activity
android:name="com.example.my_title1.MainActivity"
android:theme="@style/XTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest></span>