【安卓】自定义标题栏+PopupWindow+动画跳入跳出

一、先上效果图


二、涉及的文件


三、自定义标题栏

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>







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下代码实现PopupWindow的进动画和退出动画: ```java // 设置PopupWindow动画 popupWindow.setAnimationStyle(R.style.PopupWindowEnterAnimation); // 设置PopupWindow退出动画 popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { popupWindow.setAnimationStyle(R.style.PopupWindowExitAnimation); } }); ``` 接着,在styles.xml文件中定义动画和退出动画的样式: ```xml <style name="PopupWindowEnterAnimation"> <item name="android:windowEnterAnimation">@anim/slide_in_from_bottom</item> </style> <style name="PopupWindowExitAnimation"> <item name="android:windowExitAnimation">@anim/slide_out_to_bottom</item> </style> ``` 其中,@anim/slide_in_from_bottom和@anim/slide_out_to_bottom是自定义的进和退出动画资源文件。你可以根据自己的需求进行定义。 slide_in_from_bottom.xml: ```xml <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="@android:integer/config_mediumAnimTime"/> <alpha android:fromAlpha="0" android:toAlpha="1" android:duration="@android:integer/config_mediumAnimTime"/> </set> ``` slide_out_to_bottom.xml: ```xml <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="@android:integer/config_mediumAnimTime"/> <alpha android:fromAlpha="1" android:toAlpha="0" android:duration="@android:integer/config_mediumAnimTime"/> </set> ``` 这里的动画效果是从底部滑和滑出,你可以根据需要进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值