android中的导航菜单

时间:2016-04-30 06:10:43

标签: android android-layout

我是Android新手,但我想在我的应用程序中制作导航菜单。我成功从Web服务获取数据。但我在导航菜单中有问题如何在导航菜单中添加Web服务菜单项?  在这个活动中首先通过Web服务获取数据然后将json对象转换为数组对象然后调用printmenu函数在导航中添加菜单项我在本节中有问题我不知道如何添加菜单。

    public class MainActivity extends AppCompatActivity 
    {
         private ArrayList<MenuItems> menuModel=new ArrayList<>();
           protected void onCreate(Bundle savedInstanceState) 
           {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
           }
           public class MenuList extends AsyncTask<String,Void,Void>
           {
             Boolean result = true;
             URL url;
             HttpURLConnection urlConnection = null;
             String[] response;
             ProgressDialog dialog;
             @Override
             protected void onPreExecute() {
                super.onPreExecute();
                dialog=new ProgressDialog(MainActivity.this);
             }

             @Override
             protected Void doInBackground(String... params) 
             {
                try{
                    response = new String[params.length];
                    for(int i=0;i<params.length;i++)
                    {
                        url = new URL(params[i]);
                        urlConnection = (HttpURLConnection)       url.openConnection();
                        urlConnection.setRequestMethod("POST");
                        urlConnection.connect();
                        int responseCode = urlConnection.getResponseCode();
                        if(responseCode== HttpURLConnection.HTTP_OK)
                        {
                            InputStream in = urlConnection.getInputStream();
                            response[i] = convertStreamToString(in);
                            Log.d("Passs", response[i]+"");
                        }
                    }

                }
                catch (Exception e)
                {
                    Log.e("Error",e.getMessage());
                    result = false;
                }
                return null;
            }
            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                    if(result)
                    {
                        allMenuItems(response[0]);
                        //printCategory(response[0]);

                    }
                else {
                    Toast.makeText(MainActivity.this,"Somthing bad happen ",Toast.LENGTH_LONG).show();
                }
            }
        }
        private void allMenuItems(String response)
        {
            try
            {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("menu");
                for(int i=0;i<jsonArray.length();i++)
                {
                    JSONObject objMenu=jsonArray.getJSONObject(i);
                    MenuItems objectmenu=new MenuItems();
                    objectmenu.setMenu_id(objMenu.getString("category_id"));
                    objectmenu.setMenuitem_item(objMenu.getString("category_name"));
                    objectmenu.setMenu_image(objMenu.getString("category_banner"));
                    menuModel.add(objectmenu);
                }
                printMenu();
            }
            catch (Exception e)
            {
                e.getMessage();
            }
        }
        private void printMenu()
        {
            for(int i=0;i<=menuModel.size();i++)
            {



            }
        }
    }
XML File

<?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:clickable="true">


        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/linear_layout_menu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="10dp"
                    android:textSize="20dp"
                    android:text="Category"
                    android:textStyle="bold"
                    android:textColorHint="@color/gry"
                    android:textColor="@color/gray"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center_horizontal"
                    android:layout_toEndOf="@+id/image_view"
                    android:layout_toRightOf="@+id/image_view"
                    android:gravity="center_vertical"
                    android:paddingLeft="10dp"/>


                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:paddingTop="10dp"
                        android:paddingLeft="10dp"
                        android:id="@+id/image_view"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src = "@drawable/love"

                        />

                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:paddingTop="10dp"
                        android:textSize="19dp"
                        android:text="Ring"
                        android:textStyle="normal"
                        android:textColor="@color/lightgray"
                        android:layout_centerVertical="true"
                        android:layout_gravity="center_horizontal"
                        android:layout_toEndOf="@+id/image_view"
                        android:layout_toRightOf="@+id/image_view"
                        android:gravity="center_vertical"
                        android:paddingLeft="10dp"/>


                </RelativeLayout>



                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:paddingTop="10dp"
                        android:paddingLeft="10dp"
                        android:id="@+id/imageView52"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src = "@drawable/bracelt"/>

                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:paddingTop="10dp"
                        android:textSize="19dp"
                        android:text="Bracelet"
                        android:textStyle="normal"
                        android:textColor="@color/lightgray"
                        android:layout_centerVertical="true"
                        android:layout_gravity="center_horizontal"
                        android:layout_toEndOf="@+id/imageView52"
                        android:layout_toRightOf="@+id/imageView52"
                        android:gravity="center_vertical"
                        android:paddingLeft="10dp"/>

                </RelativeLayout>
            </LinearLayout>



            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="25dp"
                    android:background="@color/lightgray"/>

            <ImageView
                android:layout_marginTop="30dp"
                android:paddingTop="10dp"
                android:paddingLeft="6dp"
                android:id="@+id/imageView57"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src = "@drawable/ratenew"/>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="35dp"
                android:textSize="16dp"
                android:textStyle="normal"
                android:text="Rate Us"
                android:textColor="@color/lightgray"
                android:layout_centerVertical="true"
                android:layout_gravity="center_horizontal"
                android:layout_toEndOf="@+id/imageView57"
                android:layout_toRightOf="@+id/imageView57"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"/></RelativeLayout>


            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <ImageView
                android:paddingTop="10dp"
                android:paddingLeft="6dp"
                android:id="@+id/imageView58"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src = "@drawable/gestures"/>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="10dp"
                android:textSize="16dp"
                android:textStyle="normal"
                android:paddingLeft="10dp"
                android:text="Today's Deal"
                android:textColor="@color/lightgray"
                android:layout_centerVertical="true"
                android:layout_gravity="center_horizontal"
                android:layout_toEndOf="@+id/imageView58"
                android:layout_toRightOf="@+id/imageView58"
                android:gravity="center_vertical"

              />
    </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <ImageView
                android:paddingTop="10dp"
                android:paddingLeft="6dp"
                android:id="@+id/imageView59"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src = "@drawable/contect"/>

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="10dp"
                android:textSize="16dp"
                android:paddingLeft="10dp"
                android:textStyle="normal"
                android:text="Contact Us"
                android:textColor="@color/lightgray"
                android:layout_centerVertical="true"
                android:layout_gravity="center_horizontal"
                android:layout_toEndOf="@+id/imageView59"
                android:layout_toRightOf="@+id/imageView59"
                android:gravity="center_vertical"
               />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <ImageView
                android:paddingTop="10dp"
                android:id="@+id/imageView60"
                android:paddingLeft="6dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src = "@drawable/about"/>

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="10dp"
                android:textSize="16dp"
                android:paddingLeft="10dp"
                android:textStyle="normal"
                android:text="About Us"
                android:textColor="@color/lightgray"
                android:layout_centerVertical="true"
                android:layout_gravity="center_horizontal"
                android:layout_toEndOf="@+id/imageView60"
                android:layout_toRightOf="@+id/imageView60"
                android:gravity="center_vertical"
                />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="20dp"

                    android:background="@color/lightgray"/>


                <ImageView
                    android:paddingTop="5dp"
                    android:paddingLeft="90dp"
                    android:paddingRight="0dp"
                    android:layout_marginTop="30dp"
                    android:paddingBottom="7dp"
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/fb" />

                <ImageView
                    android:paddingTop="13dp"
                    android:paddingLeft="5dp"

                    android:layout_marginTop="20dp"
                    android:paddingBottom="7dp"
                    android:id="@+id/imageView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src = "@drawable/inst"
                    android:layout_toEndOf="@+id/imageView1"
                    android:layout_toRightOf="@+id/imageView1"/>

                <ImageView
                    android:paddingTop="5dp"
                    android:paddingLeft="5dp"
                    android:paddingRight="20dp"
                    android:paddingBottom="7dp"
                    android:layout_marginTop="30dp"
                    android:id="@+id/imageView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src = "@drawable/tw"
                    android:layout_toEndOf="@+id/imageView2"
                    android:layout_toRightOf="@+id/imageView2"/>

            </RelativeLayout>





        </LinearLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

onPostExecute的{​​{1}}中,拨打AsyncTask。那应该再次调用invalidateOptionsMenu()方法。在onCreateOptionsMenu中,添加onCreateOptionsMenu中的MenuItem