安卓Andriod使用入门(十二)【网络爬虫】

青春,如同一场盛大华丽的戏,真正点亮生命的不是明天的景色,而是美好的希望。最美的不是生如夏花,而是在时间的长河里,波澜不惊。有些话,适合烂在心里,有些痛苦,适合无声无息的忘记。


MainActivity.java代码:

package siso.mycrawler;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.jsoup.Jsoup;
import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MainActivity extends ActionBarActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    private NavigationDrawerFragment mNavigationDrawerFragment;
    private CharSequence mTitle;
    private ListView infoListView;
    private List<Map<String, Object>> list = new ArrayList<>();
    private String url_first_half = "http://xjh.haitou.cc/wh/uni-";
    private String url_second_half = "/after/hold/page-";
    private String next_page_url = "";
    private int currentPage = 1;
    private int currentPosition;
    private String url;
    private ProgressDialog dialog;
    // popupWindow
    private PopupWindow popupWindow;
    private ListView menuListView;

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

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();
        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

        // 显示宣讲会信息的ListView
        infoListView = (ListView) findViewById(R.id.info_list_view);

        // 初始化popupWindow
        initPopupWindow();

    }


    // 将数据填充到ListView中
    private void show() {
        if(list.isEmpty()) {
            TextView message = (TextView)findViewById(R.id.message);
            message.setText(R.string.message);
        } else {
            SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.my_list_item,
                    new String[]{"company", "time", "address"},
                    new int[]{R.id.company, R.id.time, R.id.address});
            infoListView.setAdapter(adapter);
        }
        dialog.dismiss();  // 关闭窗口
    }


    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Connection conn = Jsoup.connect(url);
            // 修改http包中的header,伪装成浏览器进行抓取
            conn.header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:32.0) Gecko/    20100101 Firefox/32.0");
            Document doc = null;
            try {
                doc = conn.get();
            } catch (IOException e) {
                e.printStackTrace();
            }

            // 获取下一页的链接
            Elements link = doc.select("li.paginate_button").select("li.next");
            next_page_url = link.select("a").attr("href");
            // 获取tbody元素下的所有tr元素
            Elements elements = doc.select("tbody tr");
            for(Element element : elements) {
                String companyName = element.getElementsByClass("text-success").text();
                String time = element.getElementsByClass("cxxt-holdtime").text();
                String address = element.getElementsByClass("text-ellipsis").text();

                Map<String, Object> map = new HashMap<>();
                map.put("company", companyName);
                map.put("time", time);
                map.put("address", address);
                list.add(map);
            }
            // 执行完毕后给handler发送一个空消息
            handler.sendEmptyMessage(0);
        }
    };


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // 收到消息后执行handler
            show();
        }
    };


    // 判断是否有可用的网络连接
    public boolean isNetworkAvailable(Activity activity)
    {
        Context context = activity.getApplicationContext();
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null)
            return false;
        else
        {   // 获取所有NetworkInfo对象
            NetworkInfo[] networkInfo = cm.getAllNetworkInfo();
            if (networkInfo != null && networkInfo.length > 0)
            {
                for (int i = 0; i < networkInfo.length; i++)
                    if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED)
                        return true;  // 存在可用的网络连接
            }
        }
        return false;
    }


    // 重新抓取
    public void switchOver(final int position) {
        if(isNetworkAvailable(MainActivity.this)) {
            // 显示“正在加载”窗口
            dialog = new ProgressDialog(this);
            dialog.setMessage("正在抓取数据...");
            dialog.setCancelable(false);
            dialog.show();

            // 初始时,抓取并显示;切换学校时,重新抓取并显示
            url = url_first_half + (position+1) + url_second_half + currentPage + "/";
            list.clear();
            new Thread(runnable).start();  // 子线程
        } else {
            // 弹出提示框
            new AlertDialog.Builder(this)
                    .setTitle("提示")
                    .setMessage("当前没有网络连接!")
                    .setPositiveButton("重试",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switchOver(position);
                        }
                    }).setNegativeButton("退出",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    System.exit(0);  // 退出程序
                }
            }).show();
        }
    }


    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                .commit();
        currentPosition = position;  // 保存当前位置(学校)
        // 切换学校
        currentPage = 1;   // 重置1
        switchOver(position);
    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.title_section1);
                break;
            case 2:
                mTitle = getString(R.string.title_section2);
                break;
            case 3:
                mTitle = getString(R.string.title_section3);
                break;
        }
    }

    public void restoreActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            getMenuInflater().inflate(R.menu.main, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }


    // 刷新
    public void refresh() {
        if(isNetworkAvailable(MainActivity.this)) {
            // 显示“正在刷新”窗口
            dialog = new ProgressDialog(this);
            dialog.setMessage("正在刷新...");
            dialog.setCancelable(false);
            dialog.show();
            // 重新抓取
            list.clear();
            new Thread(runnable).start();  // 子线程
        } else {
            // 弹出提示框
            new AlertDialog.Builder(this)
                    .setTitle("刷新")
                    .setMessage("当前没有网络连接!")
                    .setPositiveButton("重试",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            refresh();
                        }
                    }).setNegativeButton("退出",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    System.exit(0);  // 退出程序
                }
            }).show();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.refresh:
                refresh();
                return true;
            case R.id.more:
                if(popupWindow.isShowing())
                    popupWindow.dismiss();
                else
                    popUp();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    //实例化PopupWindow创建菜单
    private void initPopupWindow(){
        View view = getLayoutInflater().inflate(R.layout.popup_window, null);
        menuListView = (ListView)view.findViewById(R.id.popup_list_view);
        popupWindow = new PopupWindow(view, 160,WindowManager.LayoutParams.WRAP_CONTENT);
        // 数据
        List<Map<String, Object>> data = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("menu_text", "上一页");
        data.add(map);
        map = new HashMap<>();
        map.put("menu_text", "下一页");
        data.add(map);
        // 创建适配器
        SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.menu_list_item,
                new String[]{"menu_text"}, new int[]{R.id.menu_text});
        menuListView.setAdapter(adapter);
        // 添加Item点击响应
        menuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        prePage();  // 上一页
                        break;
                    case 1:
                        nextPage(); // 下一页
                }
            }
        });
        // 在popupWindow以外的区域点击后关闭popupWindow
        popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
    }


    //显示PopupWindow菜单
    private void popUp(){
        //设置位置
        popupWindow.showAsDropDown(this.findViewById(R.id.more), 0, 2);
    }


    // 上一页
    public void prePage() {
        if(isNetworkAvailable(MainActivity.this)) {
            if(currentPage == 1)
                Toast.makeText(MainActivity.this, "已经是第一页了", Toast.LENGTH_SHORT).show();
            else {
                --currentPage;
                switchOver(currentPosition);
            }
        } else {
            // 弹出提示框
            new AlertDialog.Builder(this)
                    .setTitle("上一页")
                    .setMessage("当前没有网络连接!")
                    .setPositiveButton("重试",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            prePage();
                        }
                    }).setNegativeButton("退出",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    System.exit(0);  // 退出程序
                }
            }).show();
        }
    }

    // 下一页
    public void nextPage() {
        if(isNetworkAvailable(MainActivity.this)) {
            if(next_page_url.equals("#"))
                Toast.makeText(MainActivity.this, "已经是最后一页了", Toast.LENGTH_SHORT).show();
            else {
                ++currentPage;
                switchOver(currentPosition);
            }
        } else {
            // 弹出提示框
            new AlertDialog.Builder(this)
                    .setTitle("下一页")
                    .setMessage("当前没有网络连接!")
                    .setPositiveButton("重试",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            nextPage();
                        }
                    }).setNegativeButton("退出",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    System.exit(0);  // 退出程序
                }
            }).show();
        }
    }


    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((MainActivity) activity).onSectionAttached(  getArguments().getInt(ARG_SECTION_NUMBER));
        }
    }

}

package siso.mycrawler;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class NavigationDrawerFragment extends Fragment {


    private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";


    private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";


    private NavigationDrawerCallbacks mCallbacks;

    private ActionBarDrawerToggle mDrawerToggle;

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerListView;
    private View mFragmentContainerView;

    private int mCurrentSelectedPosition = 0;
    private boolean mFromSavedInstanceState;
    private boolean mUserLearnedDrawer;

    public NavigationDrawerFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
        mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);

        if (savedInstanceState != null) {
            mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
            mFromSavedInstanceState = true;
        }


        selectItem(mCurrentSelectedPosition);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mDrawerListView = (ListView) inflater.inflate(
                R.layout.fragment_navigation_drawer, container, false);
        mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);
            }
        });
        mDrawerListView.setAdapter(new ArrayAdapter<String>(
                getActionBar().getThemedContext(),
                android.R.layout.simple_list_item_activated_1,
                android.R.id.text1,
                new String[]{
                        getString(R.string.title_section1),
                        getString(R.string.title_section2),
                        getString(R.string.title_section3),
                }));
        mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
        return mDrawerListView;
    }

    public boolean isDrawerOpen() {
        return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
    }

    public void setUp(int fragmentId, DrawerLayout drawerLayout) {
        mFragmentContainerView = getActivity().findViewById(fragmentId);
        mDrawerLayout = drawerLayout;


        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(
                getActivity(),                    
                mDrawerLayout,                     
                R.drawable.ic_drawer,              
                R.string.navigation_drawer_open,  
                R.string.navigation_drawer_close   
        ) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                if (!isAdded()) {
                    return;
                }

                getActivity().supportInvalidateOptionsMenu();  
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if (!isAdded()) {
                    return;
                }

                if (!mUserLearnedDrawer) {

                    mUserLearnedDrawer = true;
                    SharedPreferences sp = PreferenceManager
                            .getDefaultSharedPreferences(getActivity());
                    sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
                }

                getActivity().supportInvalidateOptionsMenu();  
            }
        };


        if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
            mDrawerLayout.openDrawer(mFragmentContainerView);
        }


        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });

        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    private void selectItem(int position) {
        mCurrentSelectedPosition = position;
        if (mDrawerListView != null) {
            mDrawerListView.setItemChecked(position, true);
        }
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mFragmentContainerView);
        }
        if (mCallbacks != null) {
            mCallbacks.onNavigationDrawerItemSelected(position);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mCallbacks = (NavigationDrawerCallbacks) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        if (mDrawerLayout != null && isDrawerOpen()) {
            inflater.inflate(R.menu.global, menu);
            showGlobalContextActionBar();
        }
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }



        return super.onOptionsItemSelected(item);
    }


    private void showGlobalContextActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setTitle(R.string.app_name);
    }

    private ActionBar getActionBar() {
        return ((ActionBarActivity) getActivity()).getSupportActionBar();
    }


    public static interface NavigationDrawerCallbacks {

        void onNavigationDrawerItemSelected(int position);
    }
}

AndroidManifest.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="siso.mycrawler">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

activity_main.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".MainActivity">


    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/info_list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@color/almost_white"
            android:dividerHeight="2dp"/>

    </FrameLayout>

    <fragment
        android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="siso.mycrawler.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

fragment_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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PlaceholderFragment">

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

fragment_navigation_drawer.xml内容:

<ListView 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:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="@color/white"
    tools:context=".NavigationDrawerFragment" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:padding="10dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/menu_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="16dip"/>

</LinearLayout>

my_list_item.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:text="@string/company"
            android:textSize="15dip"
            android:textColor="@color/grey_900"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/company"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:text="@string/time"
            android:textSize="15dip"
            android:textColor="@color/grey_900"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/time"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:text="@string/address"
            android:textSize="15dip"
            android:textColor="@color/grey_900"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

popup_window.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#364598"
    android:orientation="vertical">

    <ListView
        android:id="@+id/popup_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_settings"
        android:title="@string/more"
        android:orderInCategory="100"
        app:showAsAction="never" />
</menu>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    <item
        android:id="@+id/refresh"
        android:icon="@drawable/ic_action_refresh"
        android:title="@string/refresh"
        android:orderInCategory="1"
        app:showAsAction="ifRoom" />

    <item android:id="@+id/more"
        android:icon="@drawable/ic_action_overflow"
        android:title="@string/more"
        android:orderInCategory="2"
        app:showAsAction="ifRoom" />
</menu>

colors.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="primary">@color/orange_500</color>
    <color name="primary_dark">@color/orange_800</color>
    <color name="accent">@color/teal_500</color>
    <color name="shadow">#88000000</color>
    <color name="almost_white">#f0f0f0</color>
    <color name="white">#ffffff</color>
    <color name="black">#000000</color>

    <color name="red_50">#fde0dc</color>
    <color name="red_100">#f9bdbb</color>
    <color name="red_200">#f69988</color>
    <color name="red_300">#f36c60</color>
    <color name="red_400">#e84e40</color>
    <color name="red_500">#e51c23</color>
    <color name="red_600">#dd191d</color>
    <color name="red_700">#d01716</color>
    <color name="red_800">#c41411</color>
    <color name="red_900">#b0120a</color>
    <color name="red_a100">#ff7997</color>
    <color name="red_a200">#ff5177</color>
    <color name="red_a400">#ff2d6f</color>
    <color name="red_a700">#e00032</color>

    <color name="pink_50">#fce4ec</color>
    <color name="pink_100">#f8bbd0</color>
    <color name="pink_200">#f48fb1</color>
    <color name="pink_300">#f06292</color>
    <color name="pink_400">#ec407a</color>
    <color name="pink_500">#e91e63</color>
    <color name="pink_600">#d81b60</color>
    <color name="pink_700">#c2185b</color>
    <color name="pink_800">#ad1457</color>
    <color name="pink_900">#880e4f</color>
    <color name="pink_a100">#ff80ab</color>
    <color name="pink_a200">#ff4081</color>
    <color name="pink_a400">#f50057</color>
    <color name="pink_a700">#c51162</color>

    <color name="purple_50">#f3e5f5</color>
    <color name="purple_100">#e1bee7</color>
    <color name="purple_200">#ce93d8</color>
    <color name="purple_300">#ba68c8</color>
    <color name="purple_400">#ab47bc</color>
    <color name="purple_500">#9c27b0</color>
    <color name="purple_600">#8e24aa</color>
    <color name="purple_700">#7b1fa2</color>
    <color name="purple_800">#6a1b9a</color>
    <color name="purple_900">#4a148c</color>
    <color name="purple_a100">#ea80fc</color>
    <color name="purple_a200">#e040fb</color>
    <color name="purple_a400">#d500f9</color>
    <color name="purple_a700">#aa00ff</color>

    <color name="deep_purple_50">#ede7f6</color>
    <color name="deep_purple_100">#d1c4e9</color>
    <color name="deep_purple_200">#b39ddb</color>
    <color name="deep_purple_300">#9575cd</color>
    <color name="deep_purple_400">#7e57c2</color>
    <color name="deep_purple_500">#673ab7</color>
    <color name="deep_purple_600">#5e35b1</color>
    <color name="deep_purple_700">#512da8</color>
    <color name="deep_purple_800">#4527a0</color>
    <color name="deep_purple_900">#311b92</color>
    <color name="deep_purple_a100">#b388ff</color>
    <color name="deep_purple_a200">#7c4dff</color>
    <color name="deep_purple_a400">#651fff</color>
    <color name="deep_purple_a700">#6200ea</color>

    <color name="indigo_50">#e8eaf6</color>
    <color name="indigo_100">#c5cae9</color>
    <color name="indigo_200">#9fa8da</color>
    <color name="indigo_300">#7986cb</color>
    <color name="indigo_400">#5c6bc0</color>
    <color name="indigo_500">#3f51b5</color>
    <color name="indigo_600">#3949ab</color>
    <color name="indigo_700">#303f9f</color>
    <color name="indigo_800">#283593</color>
    <color name="indigo_900">#1a237e</color>
    <color name="indigo_a100">#8c9eff</color>
    <color name="indigo_a200">#536dfe</color>
    <color name="indigo_a400">#3d5afe</color>
    <color name="indigo_a700">#304ffe</color>

    <color name="blue_50">#e7e9fd</color>
    <color name="blue_100">#d0d9ff</color>
    <color name="blue_200">#afbfff</color>
    <color name="blue_300">#91a7ff</color>
    <color name="blue_400">#738ffe</color>
    <color name="blue_500">#5677fc</color>
    <color name="blue_600">#4e6cef</color>
    <color name="blue_700">#455ede</color>
    <color name="blue_800">#3b50ce</color>
    <color name="blue_900">#2a36b1</color>
    <color name="blue_a100">#a6baff</color>
    <color name="blue_a200">#6889ff</color>
    <color name="blue_a400">#4d73ff</color>
    <color name="blue_a700">#4d69ff</color>

    <color name="light_blue_50">#e1f5fe</color>
    <color name="light_blue_100">#b3e5fc</color>
    <color name="light_blue_200">#81d4fa</color>
    <color name="light_blue_300">#4fc3f7</color>
    <color name="light_blue_400">#29b6f6</color>
    <color name="light_blue_500">#03a9f4</color>
    <color name="light_blue_600">#039be5</color>
    <color name="light_blue_700">#0288d1</color>
    <color name="light_blue_800">#0277bd</color>
    <color name="light_blue_900">#01579b</color>
    <color name="light_blue_a100">#80d8ff</color>
    <color name="light_blue_a200">#40c4ff</color>
    <color name="light_blue_a400">#00b0ff</color>
    <color name="light_blue_a700">#0091ea</color>

    <color name="cyan_50">#e0f7fa</color>
    <color name="cyan_100">#b2ebf2</color>
    <color name="cyan_200">#80deea</color>
    <color name="cyan_300">#4dd0e1</color>
    <color name="cyan_400">#26c6da</color>
    <color name="cyan_500">#00bcd4</color>
    <color name="cyan_600">#00acc1</color>
    <color name="cyan_700">#0097a7</color>
    <color name="cyan_800">#00838f</color>
    <color name="cyan_900">#006064</color>
    <color name="cyan_a100">#84ffff</color>
    <color name="cyan_a200">#18ffff</color>
    <color name="cyan_a400">#00e5ff</color>
    <color name="cyan_a700">#00b8d4</color>

    <color name="teal_50">#e0f2f1</color>
    <color name="teal_100">#b2dfdb</color>
    <color name="teal_200">#80cbc4</color>
    <color name="teal_300">#4db6ac</color>
    <color name="teal_400">#26a69a</color>
    <color name="teal_500">#009688</color>
    <color name="teal_600">#00897b</color>
    <color name="teal_700">#00796b</color>
    <color name="teal_800">#00695c</color>
    <color name="teal_900">#004d40</color>
    <color name="teal_a100">#a7ffeb</color>
    <color name="teal_a200">#64ffda</color>
    <color name="teal_a400">#1de9b6</color>
    <color name="teal_a700">#00bfa5</color>

    <color name="green_50">#d0f8ce</color>
    <color name="green_100">#a3e9a4</color>
    <color name="green_200">#72d572</color>
    <color name="green_300">#42bd41</color>
    <color name="green_400">#2baf2b</color>
    <color name="green_500">#259b24</color>
    <color name="green_600">#0a8f08</color>
    <color name="green_700">#0a7e07</color>
    <color name="green_800">#056f00</color>
    <color name="green_900">#0d5302</color>
    <color name="green_a100">#a2f78d</color>
    <color name="green_a200">#5af158</color>
    <color name="green_a400">#14e715</color>
    <color name="green_a700">#12c700</color>

    <color name="light_green_50">#f1f8e9</color>
    <color name="light_green_100">#dcedc8</color>
    <color name="light_green_200">#c5e1a5</color>
    <color name="light_green_300">#aed581</color>
    <color name="light_green_400">#9ccc65</color>
    <color name="light_green_500">#8bc34a</color>
    <color name="light_green_600">#7cb342</color>
    <color name="light_green_700">#689f38</color>
    <color name="light_green_800">#558b2f</color>
    <color name="light_green_900">#33691e</color>
    <color name="light_green_a100">#ccff90</color>
    <color name="light_green_a200">#b2ff59</color>
    <color name="light_green_a400">#76ff03</color>
    <color name="light_green_a700">#64dd17</color>

    <color name="lime_50">#f9fbe7</color>
    <color name="lime_100">#f0f4c3</color>
    <color name="lime_200">#e6ee9c</color>
    <color name="lime_300">#dce775</color>
    <color name="lime_400">#d4e157</color>
    <color name="lime_500">#cddc39</color>
    <color name="lime_600">#c0ca33</color>
    <color name="lime_700">#afb42b</color>
    <color name="lime_800">#9e9d24</color>
    <color name="lime_900">#827717</color>
    <color name="lime_a100">#f4ff81</color>
    <color name="lime_a200">#eeff41</color>
    <color name="lime_a400">#c6ff00</color>
    <color name="lime_a700">#aeea00</color>

    <color name="yellow_50">#fffde7</color>
    <color name="yellow_100">#fff9c4</color>
    <color name="yellow_200">#fff59d</color>
    <color name="yellow_300">#fff176</color>
    <color name="yellow_400">#ffee58</color>
    <color name="yellow_500">#ffeb3b</color>
    <color name="yellow_600">#fdd835</color>
    <color name="yellow_700">#fbc02d</color>
    <color name="yellow_800">#f9a825</color>
    <color name="yellow_900">#f57f17</color>
    <color name="yellow_a100">#ffff8d</color>
    <color name="yellow_a200">#ffff00</color>
    <color name="yellow_a400">#ffea00</color>
    <color name="yellow_a700">#ffd600</color>

    <color name="amber_50">#fff8e1</color>
    <color name="amber_100">#ffecb3</color>
    <color name="amber_200">#ffe082</color>
    <color name="amber_300">#ffd54f</color>
    <color name="amber_400">#ffca28</color>
    <color name="amber_500">#ffc107</color>
    <color name="amber_600">#ffb300</color>
    <color name="amber_700">#ffa000</color>
    <color name="amber_800">#ff8f00</color>
    <color name="amber_900">#ff6f00</color>
    <color name="amber_a100">#ffe57f</color>
    <color name="amber_a200">#ffd740</color>
    <color name="amber_a400">#ffc400</color>
    <color name="amber_a700">#ffab00</color>

    <color name="orange_50">#fff3e0</color>
    <color name="orange_100">#ffe0b2</color>
    <color name="orange_200">#ffcc80</color>
    <color name="orange_300">#ffb74d</color>
    <color name="orange_400">#ffa726</color>
    <color name="orange_500">#ff9800</color>
    <color name="orange_600">#fb8c00</color>
    <color name="orange_700">#f57c00</color>
    <color name="orange_800">#ef6c00</color>
    <color name="orange_900">#e65100</color>
    <color name="orange_a100">#ffd180</color>
    <color name="orange_a200">#ffab40</color>
    <color name="orange_a400">#ff9100</color>
    <color name="orange_a700">#ff6d00</color>

    <color name="deep_orange_50">#fbe9e7</color>
    <color name="deep_orange_100">#ffccbc</color>
    <color name="deep_orange_200">#ffab91</color>
    <color name="deep_orange_300">#ff8a65</color>
    <color name="deep_orange_400">#ff7043</color>
    <color name="deep_orange_500">#ff5722</color>
    <color name="deep_orange_600">#f4511e</color>
    <color name="deep_orange_700">#e64a19</color>
    <color name="deep_orange_800">#d84315</color>
    <color name="deep_orange_900">#bf360c</color>
    <color name="deep_orange_a100">#ff9e80</color>
    <color name="deep_orange_a200">#ff6e40</color>
    <color name="deep_orange_a400">#ff3d00</color>
    <color name="deep_orange_a700">#dd2c00</color>

    <color name="brown_50">#efebe9</color>
    <color name="brown_100">#d7ccc8</color>
    <color name="brown_200">#bcaaa4</color>
    <color name="brown_300">#a1887f</color>
    <color name="brown_400">#8d6e63</color>
    <color name="brown_500">#795548</color>
    <color name="brown_600">#6d4c41</color>
    <color name="brown_700">#5d4037</color>
    <color name="brown_800">#4e342e</color>
    <color name="brown_900">#3e2723</color>

    <color name="grey_50">#fafafa</color>
    <color name="grey_100">#f5f5f5</color>
    <color name="grey_200">#eeeeee</color>
    <color name="grey_300">#e0e0e0</color>
    <color name="grey_400">#bdbdbd</color>
    <color name="grey_500">#9e9e9e</color>
    <color name="grey_600">#757575</color>
    <color name="grey_700">#616161</color>
    <color name="grey_800">#424242</color>
    <color name="grey_900">#212121</color>

    <color name="blue_grey_50">#eceff1</color>
    <color name="blue_grey_100">#cfd8dc</color>
    <color name="blue_grey_200">#b0bec5</color>
    <color name="blue_grey_300">#90a4ae</color>
    <color name="blue_grey_400">#78909c</color>
    <color name="blue_grey_500">#607d8b</color>
    <color name="blue_grey_600">#546e7a</color>
    <color name="blue_grey_700">#455a64</color>
    <color name="blue_grey_800">#37474f</color>
    <color name="blue_grey_900">#263238</color>
</resources>

运行效果如图:

这里写图片描述


这里写图片描述


这里写图片描述


这里写图片描述

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值