編輯:關於android開發
1. share_item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#00ffffff" android:padding="30dp" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/imageView1" android:layout_centerHorizontal="true" android:layout_marginTop="2dp" android:text="TextView"/> </RelativeLayout>2.
share_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#00ffffff" android:padding="30dp" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/imageView1" android:layout_centerHorizontal="true" android:layout_marginTop="2dp" android:text="TextView"/> </RelativeLayout>
3. ShareDialog.java
package com.zps.sharedemo; import android.app.AlertDialog; import android.content.Context; import android.view.Gravity; import android.view.View.OnClickListener; import android.view.Window; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.RelativeLayout; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class ShareDialog { private AlertDialog dialog; private GridView gridView; private RelativeLayout cancelButton; private SimpleAdapter saImageItems; private int[] image = { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4 }; private String[] name = { "微博", "微信好友", "朋友圈", "QQ" }; public ShareDialog(Context context) { dialog = new AlertDialog.Builder(context).create(); dialog.show(); Window window = dialog.getWindow(); window.setGravity(Gravity.BOTTOM); // 非常重要:設置對話框彈出的位置 window.setContentView(R.layout.share_dialog); gridView = (GridView) window.findViewById(R.id.share_gridView); cancelButton = (RelativeLayout) window.findViewById(R.id.share_cancel); List<HashMap<String, Object>> shareList = new ArrayList<HashMap<String, Object>>(); for (int i = 0; i < image.length; i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("ItemImage", image[i]);// 添加圖像資源的ID map.put("ItemText", name[i]);// 按序號做ItemText shareList.add(map); } saImageItems = new SimpleAdapter(context, shareList, R.layout.share_item, new String[] { "ItemImage", "ItemText" }, new int[] { R.id.imageView1, R.id.textView1 }); gridView.setAdapter(saImageItems); } public void setCancelButtonOnClickListener(OnClickListener Listener) { cancelButton.setOnClickListener(Listener); } public void setOnItemClickListener(OnItemClickListener listener) { gridView.setOnItemClickListener(listener); } /** * 關閉對話框 */ public void dismiss() { dialog.dismiss(); } }
4. MainActivity.java
package com.zps.sharedemo; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener { private Button shareButton; ShareDialog shareDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { shareButton = (Button) findViewById(R.id.shareButton); shareButton.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.shareButton: shareDialog = new ShareDialog(this); shareDialog.setCancelButtonOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { shareDialog.dismiss(); } }); shareDialog.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { HashMap<String, Object> item = (HashMap<String, Object>) arg0.getItemAtPosition(arg2); if (item.get("ItemText").equals("微博")) { } else if (item.get("ItemText").equals("微信好友")) { } else if (item.get("ItemText").equals("朋友圈")) { } else if (item.get("ItemText").equals("QQ")) { } shareDialog.dismiss(); } }); break; default: break; } } }
5. activity_main.xml
<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=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/shareButton" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
Android Material Design:NavigationView抽屜導航菜單,materialdesign 需要添加的包: 測試
Android draw9patch 圖片制作與使用,理解一下4句話: 下面我拿6張圖片分別舉例說明: 1.QQ多彩氣泡 聊天對話框也用.9
Android java層音頻相關的分析與理解(三)調用相關 Android中會有多個應用需要支持音頻的播放。當同一時間內有多個應用需要輸出音頻的時候,到底是全部輸出
Android自定義控件來襲(Scroller) 先看看效果圖 實現方法繼承自ViewGroup需要我們自己來測量,布局,實現滑動的效果,處理滑動沖突, 自定義Vi