android 使用Activity做窗口弹出(模拟Dialog)
我们下面使用Activity,模拟一个dialog:
首先看布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="270dp" android:layout_height="wrap_content" android:gravity="center_horizontal" > <!--标题--> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="分享到哪里?" android:textSize="18sp" android:padding="10dp" android:textColor="@color/dialog_title_color" android:gravity="center_horizontal" /> <!--分割线--> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/bg_action_bar_color" /> <!--分享到哪里--> <GridView android:id="@+id/dialog_share_grid_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:listSelector="@drawable/empty_selector" android:numColumns="3" android:horizontalSpacing="10dp" android:verticalSpacing="15dp" android:layout_margin="10dp" /> </LinearLayout>
然后是该Activity
package cn.via.dageeeOrderFood.activity; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; import cn.via.dageeeOrderFood.R; /** * Created by Heyiyong on 2014-3-5 上午9:12. */ public class ShareActivity extends Activity { private int[] shareIcons = { R.drawable.ic_share_duanxin, R.drawable.ic_share_pengyouquan, R.drawable.ic_share_qq, R.drawable.ic_share_qqkongjian, R.drawable.ic_share_weixin, }; private String[] shareTitles = { "短信", "朋友圈", "QQ好友", "QQ空间", "微信" }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_share); GridView gridView = (GridView) findViewById(R.id.dialog_share_grid_view); gridView.setAdapter(new BaseAdapter() { @Override public int getCount() { return shareIcons.length; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { View itemView = View.inflate(ShareActivity.this, R.layout.item_share_grid_view, null); ((ImageView) itemView.findViewById(R.id.item_share_img)).setImageResource(shareIcons[i]); ((TextView) itemView.findViewById(R.id.item_share_tv)).setText(shareTitles[i]); return itemView; } }); } }
gridView的item布局
<?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" android:gravity="center_horizontal" android:background="@drawable/share_grid_view_item_selector_xml" > <ImageView android:id="@+id/item_share_img" android:layout_width="56dp" android:layout_height="56dp" android:src="@drawable/ic_share_duanxin" android:padding="4dp" /> <TextView android:id="@+id/item_share_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="短信" android:textSize="13sp" android:layout_marginTop="3dp" android:textColor="@color/dialog_content_color" /> </LinearLayout>
最后在AndroidManifest文件:
<!--分享的dialogActivity--> <activity android:name=".activity.ShareActivity" android:theme="@style/dialogActivity" />
其中theme为:
<!--用activity模拟dialog--> <style name="dialogActivity"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowBackground">@drawable/dialog_bg_update</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:backgroundDimEnabled">true</item> </style>
drawable/dialog_bg_update.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/dialog_background_color"/> <!--<stroke android:color="#ff5e5e5e" android:width="1dp"/>--> <stroke android:color="@color/bg_action_bar_color" android:width="1dp"/> <corners android:radius="10dp"/> </shape>
drawable/share_grid_view_item_selector_xml.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/item_press_color"/> <corners android:radius="5dp"/> </shape> </item> <item> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/dialog_background_color"/> </shape> </item> </selector>
本文出自 无忧之路 - 博客园