首页 > 代码库 > 自定义归属地风格弹窗
自定义归属地风格弹窗
package com.loaderman.addressdialogdemo; import android.app.Dialog; import android.content.Context; import android.view.Gravity; import android.view.Window; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ListView; /** * * <p/> * 自定义归属地风格弹窗 * <p/> * 1. 给dialog设置布局,setContentView * 2. 去掉标题栏和背景 * 去掉标题栏 * <item name="android:windowNoTitle">true</item> * 去掉背景 * <item name="android:windowBackground">@color/white</item> * <p/> * 3. dialog显示在屏幕下方, 修改窗口的位置 */ public class AddressDialog extends Dialog { private ListView lvList; public AddressDialog(Context context) { //指定dialog的样式 super(context, R.style.AddressDialogStyle); //给dialog设置布局 setContentView(R.layout.dialog_address); //显示在屏幕正下方 //原理: 修改dialog所在窗口Window的位置, dialog随窗口显示 Window window = getWindow();//获取dialog所在的窗口对象 WindowManager.LayoutParams attributes = window.getAttributes();//获取当前窗口的属性(布局参数) attributes.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;//靠下居中显示 window.setAttributes(attributes);//重新设置布局参数 lvList = (ListView) findViewById(R.id.lv_list); } //为了增强扩展性, 可以由外部传递adapter进来, 决定ListView的显示内容 public void setAdapter(BaseAdapter adapter) { lvList.setAdapter(adapter); } //设置条目点击事件 public void setOnItemClickListener(AdapterView.OnItemClickListener listener) { lvList.setOnItemClickListener(listener); } }
package com.loaderman.addressdialogdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private int[] mIcons = new int[]{R.drawable.shape_address_normal, R.drawable .shape_address_orange, R.drawable.shape_address_blue, R.drawable.shape_address_gray, R.drawable.shape_address_green}; private String[] mNames = new String[]{"半透明", "活力橙", "卫士蓝", "金属灰", "苹果绿"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void set(View view) { //显示弹窗 //dialog中传的context必须是activity对象 final AddressDialog dialog = new AddressDialog(MainActivity.this); dialog.setAdapter(new AddressStyleAdapter()); dialog.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //1. 记录当前被选中的条目的位置, 保存在sp中 PrefUtils.putInt(getApplicationContext(), "address_style", position); //2. 隐藏弹窗 dialog.dismiss(); } }); dialog.show(); } //归属地样式弹窗adapter class AddressStyleAdapter extends BaseAdapter { @Override public int getCount() { return mNames.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = View.inflate(MainActivity.this, R.layout.item_address, null); ImageView ivIcon = (ImageView) view.findViewById(R.id.iv_icon); TextView tvName = (TextView) view.findViewById(R.id.tv_name); ImageView ivSelect = (ImageView) view.findViewById(R.id.iv_select); ivIcon.setImageResource(mIcons[position]); tvName.setText(mNames[position]); //从sp中取出当前选中的样式位置 //和当前position比较 int pos = PrefUtils.getInt(getApplicationContext(),"address_style", 0);//默认是半透明 if (position == pos) { //当前条目被选中 //显示勾选图片 ivSelect.setVisibility(View.VISIBLE); } else { ivSelect.setVisibility(View.GONE); } return view; } } }
package com.loaderman.addressdialogdemo; import android.content.Context; import android.content.SharedPreferences; public class PrefUtils { public static void putInt(Context ctx, String key, int value) { SharedPreferences sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE); sp.edit().putInt(key, value).commit(); } public static int getInt(Context ctx, String key, int defValue) { SharedPreferences sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE); return sp.getInt(key, defValue); } }
在res/anim下
<!--anim_dialog_enter--> <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <alpha android:duration="@integer/abc_config_activityShortDur" android:fromAlpha="0.0" android:interpolator="@android:anim/decelerate_interpolator" android:toAlpha="1.0"/> <translate android:duration="150" android:fromYDelta="70%" android:toYDelta="0" /> </set>
<!--anim_dialog_exit--> <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <alpha android:duration="@integer/abc_config_activityShortDur" android:fromAlpha="1.0" android:interpolator="@android:anim/decelerate_interpolator" android:toAlpha="0.0"/> <translate android:duration="150" android:fromYDelta="0" android:toYDelta="70%" /> </set>
activity_main.xml
<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" tools:context="com.loaderman.addressdialogdemo.MainActivity"> <Button android:onClick="set" android:id="@+id/btn_set" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="风格样式设置"/> </LinearLayout>
dialog_address.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="选择归属地样式" android:id="@+id/textView2" android:background="#cccccc" android:padding="8dp" android:textColor="@color/black" android:textSize="18sp" android:layout_gravity="center_horizontal"/> <ListView android:layout_width="match_parent" android:layout_height="200dp" android:id="@+id/lv_list"/> </LinearLayout>
item_address.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="8dp"> <ImageView android:id="@+id/iv_icon" android:layout_width="60dp" android:layout_height="30dp" android:layout_centerVertical="true" android:src="http://www.mamicode.com/@drawable/shape_address_normal"/> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/iv_icon" android:text="半透明" android:textColor="@color/black" android:textSize="18sp" /> <ImageView android:id="@+id/iv_select" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:src="http://www.mamicode.com/@drawable/ic_selected" /> </RelativeLayout>
在res/drawable下
shape_address_blue.xml shape_address_gray.xml shape_address_green.xml shape_normal_blue.xml shape_address_orange.xml只需要把下面代码需要以下color即可
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--颜色--> <solid android:color="@color/global_blue"/> <!--给矩形加圆角--> <corners android:radius="6dp"/> </shape>
在res/values/colors.xml添加需求颜色值即可如
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="red">#F00</color> <color name="black">#000</color> <color name="gray">#a000</color> <color name="global_blue">#459FD7</color> <color name="white">#fff</color> <color name="light_gray">#cccccc</color> <color name="light_green">#00ffa1</color> </resources>
在res/values/style.xml中在resources节点下添加style
<!--归属地弹窗样式--> <style name="AddressDialogStyle" parent="Theme.AppCompat.Dialog"> <!--去掉标题栏--> <item name="android:windowNoTitle">true</item> <!--去掉背景--> <item name="android:windowBackground">@color/white</item> <!--设置进入退出动画--> <item name="android:windowAnimationStyle">@style/AddressAnimStyle</item> </style> <!--归属地弹窗动画--> <style name="AddressAnimStyle"> <item name="android:windowEnterAnimation">@anim/anim_dialog_enter</item> <item name="android:windowExitAnimation">@anim/anim_dialog_exit</item> </style>
效果图:
自定义归属地风格弹窗
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。