ActionBar配合简单自定义Pop使用

时间:2022-08-18 19:10:27

最近公司项目用到了actionBar然后因需求又要配合自定义pop才能实现,最后实现了,写下来记录下,方便以后查看。
那先来看看实现效果吧

ActionBar配合简单自定义Pop使用

项目结构也是很简单的,就是自己从项目中抽取了一个小demo.

ActionBar配合简单自定义Pop使用

下面直接上代码
MainActivity代码

package wlj.com.actionbar;


import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

/**
* @author wlj
* @date 2016/7/30
*/

public class MainActivity extends Activity implements View.OnClickListener {

private android.app.ActionBar actionBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initBar();
}

/**
* 初始化action bar
*/

private void initBar() {
//得到Actionbar
actionBar = getActionBar();
//显示actionbar的返回箭头
actionBar.setDisplayHomeAsUpEnabled(true);
//不显示应用图标
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayUseLogoEnabled(true);
//设置标题名字
actionBar.setTitle("ActionBar");
//设置Actionbar的背景
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.green_bar));
}

/**
* 添加bar 按钮
*
* @param menu
* @return
*/

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
return true;
}

/**
* 根据选择的id做出对应的判断
*
* @param item actionbar id
* @return
*/

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
//系统默认的左上角
case android.R.id.home:
onBackPressed();
break;
case R.id.show_data1:
//自定义popupwidow
PopuWindow();
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* 自定义pop
*/

public void PopuWindow() {
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(MainActivity.LAYOUT_INFLATER_SERVICE);
View inflate = inflater.inflate(R.layout.pop, null);
TextView search1 = (TextView) inflate.findViewById(R.id.search_tv1);
TextView search2 = (TextView) inflate.findViewById(R.id.search_tv2);
TextView search3 = (TextView) inflate.findViewById(R.id.search_tv3);
TextView search4 = (TextView) inflate.findViewById(R.id.search_tv4);
TextView search5 = (TextView) inflate.findViewById(R.id.search_tv5);
search1.setOnClickListener(this);
search2.setOnClickListener(this);
search3.setOnClickListener(this);
search4.setOnClickListener(this);
search5.setOnClickListener(this);
PopupWindow popupWindow = new PopupWindow(this);
int height = MainActivity.this.getWindowManager().getDefaultDisplay().getHeight();
int width = MainActivity.this.getWindowManager().getDefaultDisplay().getWidth();
// 设置SelectPicPopupWindow的View
popupWindow.setContentView(inflate);
// 设置SelectPicPopupWindow的View
popupWindow.setWidth(width / 3 + 50);
// 设置SelectPicPopupWindow弹出窗体的高
popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
// 设置SelectPicPopupWindow弹出窗体可点击
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
// 刷新状态
popupWindow.update();
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0000000000);
// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
popupWindow.setBackgroundDrawable(dw);
View viewById = findViewById(R.id.show_data1);
popupWindow.showAsDropDown(viewById, viewById.getLayoutParams().width / 2, 0);
}

/**
* 自定义pop点击事件
*
* @param v 根据id
*/

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.search_tv1:
// 选项1
Toast.makeText(this, "选项1", Toast.LENGTH_SHORT).show();
break;
case R.id.search_tv2:
// 选项2
Toast.makeText(this, "选项2", Toast.LENGTH_SHORT).show();
break;
case R.id.search_tv3:
// 选项3
Toast.makeText(this, "选项3", Toast.LENGTH_SHORT).show();
break;
case R.id.search_tv4:
// 选项4
Toast.makeText(this, "选项4", Toast.LENGTH_SHORT).show();
break;
case R.id.search_tv5:
// 选项5
Toast.makeText(this, "选项5", Toast.LENGTH_SHORT).show();
break;
}
}
}

activity_main代码

<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="wlj.com.actionbar.MainActivity">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />

</RelativeLayout>

自定义pop样式代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/green_bar"
android:gravity="center_horizontal"
android:orientation="vertical">


<TextView
android:id="@+id/search_tv1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:padding="8dp"
android:text="选项1"
android:textColor="#ffffff"
android:textSize="18sp" />


<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#22000000" />


<TextView
android:id="@+id/search_tv2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:padding="8dp"
android:text="选项2"
android:textColor="#ffffff"
android:textSize="18sp"

/>


<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#22000000" />


<TextView
android:id="@+id/search_tv3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:padding="8dp"
android:text="选项3"
android:textColor="#ffffff"
android:textSize="18sp"

/>


<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#22000000" />


<TextView
android:id="@+id/search_tv4"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:padding="8dp"
android:text="选项4"
android:textColor="#ffffff"
android:textSize="18sp"

/>


<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#22000000" />


<TextView
android:id="@+id/search_tv5"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:padding="8dp"
android:text="选项5"
android:textColor="#ffffff"
android:textSize="18sp"

/>

</LinearLayout>

menu中代码

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/show_data1"
android:icon="@drawable/abc_ic_menu_moreoverflow_normal_holo_dark"
android:showAsAction="always" />

</menu>

代码是就以上这些,不过有以下一些地方需要注意的
MainActivity继承自Activity
AndroidManifest中给MainActivity设置主题
android:theme=”@android:style/Theme.Holo.Light.DarkActionBar”
好了,最后我们再完整的看一遍演示效果

ActionBar配合简单自定义Pop使用

ps 如果觉得对你有用,就顶一下吧。
版权声明:本文出自博物君子的博客,转载必须注明出处 http://blog.csdn.net/mynamelijun/article/details/52075591