如何在Android中自定义微调器? [英] how to customize the spinner in android?

查看:108
本文介绍了如何在Android中自定义微调器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个微调框,但是在将图像放入微调框时遇到了麻烦。.
i想要这样的微调框:

i am making a spinner but i am having trouble in putting the image in spinner.. i want the spinner like this:

给出了外壳和插件,但我不知道该怎么做..任何人都可以帮忙。

where crust and addon are given but i dont know how to do that ..can anybody help.

这是我的xml文件:

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical">
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_marginTop="10dp"

            android:gravity="center"
            android:orientation="vertical"
            android:paddingBottom="-50dp" >

            <!---add your views here-->
        </LinearLayout>

        <ImageView
            android:id="@+id/imageView_close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|right"
            android:clickable="true"
            android:background="@drawable/crss" />
    </FrameLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp">

        <ImageView
            android:id="@+id/desimage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/logo" />

        <TextView
            android:id="@+id/h1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Chicken Pizza Small"
            android:textSize="21sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/h2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Chicken Pizza Small Combo"
            android:textColor="#8c8181"
            android:textSize="16sp" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            android:weightSum="10">

            <Button
                android:id="@+id/quant"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginRight="10dp"
                android:layout_weight="3"
                android:background="@drawable/mybutton"
                android:text="Quantity"
                android:textAllCaps="false"
                android:textColor="#ffffff" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_weight="7"
                android:background="@drawable/stroke_button"
                android:orientation="horizontal"
                android:weightSum="3">

                <Button
                    android:id="@+id/incr"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@android:color/transparent"
                    android:text="-"
                    android:textColor="#FFA726"
                    android:textSize="25dp" />

                <View
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:background="#c0c0c0" />

                <TextView
                    android:id="@+id/value"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:inputType="number"
                    android:text="1" />

                <View
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:background="#c0c0c0" />

                <Button
                    android:id="@+id/decr"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@android:color/transparent"
                    android:text="+"
                    android:textColor="#FFA726"
                    android:textSize="25dp" />

            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp">

            <CheckBox
                android:id="@+id/sl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:buttonTint="#c0c0c0"
                android:text="SL"
                android:textColor="#8c8181"
                tools:targetApi="lollipop" />

            <CheckBox
                android:id="@+id/s"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:buttonTint="#c0c0c0"
                android:text="S"
                android:textColor="#8c8181"
                tools:targetApi="lollipop" />

            <CheckBox
                android:id="@+id/m"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:buttonTint="#c0c0c0"
                android:text="M"
                android:textColor="#8c8181"
                tools:targetApi="lollipop" />

            <CheckBox
                android:id="@+id/L"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:buttonTint="#c0c0c0"
                android:text="l"
                android:textColor="#8c8181"
                tools:targetApi="lollipop" />

            <CheckBox
                android:id="@+id/f"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:buttonTint="#c0c0c0"
                android:text="F"
                android:textColor="#8c8181"
                tools:targetApi="lollipop" />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:background="#c0c0c0" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="35dp"
                android:layout_marginLeft="7dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                android:background="@drawable/stroke_button">

                <Spinner
                    android:id="@+id/spin_1"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@android:color/transparent"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                <ImageView
                    android:id="@+id/img_1"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center_vertical"
                    android:layout_margin="5dp"

                    android:src="@drawable/spinpres" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="35dp"
                android:layout_marginLeft="7dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                android:background="@drawable/stroke_button">

                <Spinner
                    android:id="@+id/spin_2"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@android:color/transparent"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                <ImageView
                    android:id="@+id/img_2"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center_vertical"
                    android:layout_margin="5dp"

                    android:src="@drawable/spinpres" />
            </LinearLayout>
        </LinearLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="SPECIAL INSTRUCTIONS"
            android:textSize="16sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/stroke_button"
            android:padding="@dimen/fab_margin1">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/transparent"
                android:gravity="top"
                android:inputType="textCapSentences|textMultiLine"
                android:lines="5"
                android:maxLines="5"
                android:padding="2dp"
                android:textColor="#c0c0c0" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"

            android:layout_marginTop="5dp">

            <Button
                android:id="@+id/canc"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:background="@drawable/mybutton2"
                android:text="Cancel"
                android:textColor="#ffffff" />

            <Button
                android:id="@+id/ok"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_weight="1"
                android:background="@drawable/mybutton"
                android:text="Ok"
                android:textColor="#ffffff" />


        </LinearLayout>
    </LinearLayout>
</ScrollView>

这是我的子菜单活动:

public class SubMenu extends AppCompatActivity {

    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String RANK = "id";
    static String COUNTRY = "name";

    static String FLAG = "image";
    Integer i = 1;

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


        setContentView(R.layout.activity_sub_menu);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        String SelectedId = getIntent().getStringExtra("id");


        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        // Get the view from listview_main.xml

        // Execute DownloadJSON AsyncTask
        new DownloadJSON().execute();
    }

    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> implements AdapterView.OnItemClickListener {

        // @Override
        //  protected void onPreExecute() {
        //  super.onPreExecute();
        // Create a progressdialog
        //   mProgressDialog = new ProgressDialog(SubMenu.this);
        // Set progressdialog title
        //   mProgressDialog.setTitle("Categories of Main categories.....");
        // Set progressdialog message
        //  mProgressDialog.setMessage("Loading...");
        //  mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        //  mProgressDialog.show();
        // }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonarray = JsonFunctions
                    .getJSONfromURL("http://cloud....com/broccoli/menu_typeitem.php?id=" + getIntent().getStringExtra("id"));
            try {
                // Locate the array name in JSON
//                    jsonarray = jsonobject.getJSONArray("main_menu_items");


                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();

                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    // map.put("id", jsonobject.getString("id"));
                    map.put("name", jsonobject.getString("name"));

                    map.put("image", jsonobject.getString("image"));
                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.list1);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(SubMenu.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            listview.setOnItemClickListener(this);
            // Close the progressdialog
            // mProgressDialog.dismiss();
        }


        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long rowId) {


            final AlertDialog customDialog = new AlertDialog.Builder(SubMenu.this).create();
            final LayoutInflater inflater = getLayoutInflater();
            final View dialogView = inflater.inflate(R.layout.popup
                    , null);
            customDialog.setView(dialogView);
            // set the custom customDialogimation components - text, image and button
            final TextView tvDistance = (TextView) dialogView.findViewById(R.id.h2);

            final Button ok = (Button) dialogView.findViewById(R.id.ok);
            ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    customDialog.dismiss();


                }
            });


            final Button cncl = (Button) dialogView.findViewById(R.id.canc);
            cncl.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    customDialog.dismiss();


                }
            });


            Button _decrease = (Button) dialogView.findViewById(R.id.incr);
            Button _increase = (Button) dialogView.findViewById(R.id.decr);
            final TextView _value = (TextView) dialogView.findViewById(R.id.value);
            i = Integer.parseInt(_value.getText().toString());


            _decrease.setOnClickListener(new View.OnClickListener() {


                public void onClick(View v) {
                    String _stringVal;
                    Log.d("src", "Decreasing value...");
                    if (i > 0) {
                        i = i - 1;
                        _stringVal = String.valueOf(i);
                        _value.setText(_stringVal);
                    } else {
                        Log.d("src", "Value can't be less than 0");
                    }

                }
            });

            _increase.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    String _stringVal;

                    Log.d("src", "Increasing value...");
                    i = i + 1;
                    _stringVal = String.valueOf(i);
                    _value.setText(_stringVal);
                }
            });
            customDialog.show();
        }


    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

在tahmid回答之后,它看起来像这:

after tahmid's answer it looks like this:

透明度后我编辑的图片

推荐答案

在这里,我找到了解决方案,请检查您的新的xml文件

Here, I got the solution please check out your new xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="15dp"
    android:background="#F4F4F4">

    <!--Main Body of your custom dialog-->

    <ScrollView 
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
     android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

    <ImageView
        android:id="@+id/desimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/logo" />

    <TextView
        android:id="@+id/h1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="Chicken Pizza Small"
        android:textSize="21sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/h2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="Chicken Pizza Small Combo"
        android:textColor="#8c8181"
        android:textSize="16sp" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        android:weightSum="10">

        <Button
            android:id="@+id/quant"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginRight="10dp"
            android:layout_weight="3"
            android:background="@drawable/mybutton"
            android:text="Quantity"
            android:textAllCaps="false"
            android:textColor="#ffffff" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="7"
            android:background="@drawable/stroke_button"
            android:orientation="horizontal"
            android:weightSum="3">

            <Button
                android:id="@+id/incr"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/transparent"
                android:text="-"
                android:textColor="#FFA726"
                android:textSize="25dp" />

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#c0c0c0" />

            <TextView
                android:id="@+id/value"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:inputType="number"
                android:text="1" />

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#c0c0c0" />

            <Button
                android:id="@+id/decr"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/transparent"
                android:text="+"
                android:textColor="#FFA726"
                android:textSize="25dp" />

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp">

        <CheckBox
            android:id="@+id/sl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:buttonTint="#c0c0c0"
            android:text="SL"
            android:textColor="#8c8181"
            tools:targetApi="lollipop" />

        <CheckBox
            android:id="@+id/s"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:buttonTint="#c0c0c0"
            android:text="S"
            android:textColor="#8c8181"
            tools:targetApi="lollipop" />

        <CheckBox
            android:id="@+id/m"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:buttonTint="#c0c0c0"
            android:text="M"
            android:textColor="#8c8181"
            tools:targetApi="lollipop" />

        <CheckBox
            android:id="@+id/L"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:buttonTint="#c0c0c0"
            android:text="l"
            android:textColor="#8c8181"
            tools:targetApi="lollipop" />

        <CheckBox
            android:id="@+id/f"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:buttonTint="#c0c0c0"
            android:text="F"
            android:textColor="#8c8181"
            tools:targetApi="lollipop" />

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:background="#c0c0c0" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="35dp"
            android:layout_marginLeft="7dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="@drawable/stroke_button">

            <Spinner
                android:id="@+id/spin_1"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:background="@android:color/transparent"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <ImageView
                android:id="@+id/img_1"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"

                android:src="@drawable/spinpres" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="35dp"
            android:layout_marginLeft="7dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="@drawable/stroke_button">

            <Spinner
                android:id="@+id/spin_2"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:background="@android:color/transparent"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <ImageView
                android:id="@+id/img_2"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"

                android:src="@drawable/spinpres" />
        </LinearLayout>
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="SPECIAL INSTRUCTIONS"
        android:textSize="16sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/stroke_button"
        android:padding="@dimen/fab_margin1">

         <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:gravity="top"
            android:inputType="textCapSentences|textMultiLine"
            android:lines="5"
            android:maxLines="5"
            android:padding="2dp"
            android:textColor="#c0c0c0" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"

        android:layout_marginTop="5dp">

        <Button
            android:id="@+id/canc"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:background="@drawable/mybutton2"
            android:text="Cancel"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/ok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:background="@drawable/mybutton"
            android:text="Ok"
            android:textColor="#ffffff" />


            </LinearLayout>
       </LinearLayout>
   </ScrollView>
</RelativeLayout>

<LinearLayout
    android:id="@+id/llTop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical">

    <ImageButton
        android:id="@+id/btnCancel"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_below="@+id/btnBookK"
        android:background="@null"
        android:src="@drawable/close_button_image" />

</LinearLayout>

</RelativeLayout>

And if you use transparent background image it would look good ; And don’t forget to write the following code in the builder

And if you use transparent background image it would look good ; And don't forget to write the following code in the builder

AlertDialog yourDialog = builder.Create() ;
yourDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
yourDialog.show();

This code works, I tried. Hope it helps!

This code works, I tried. Hope it helps!

这篇关于如何在Android中自定义微调器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆