Android:文本不会出现在自定义下拉菜单中,但会显示相应的图标

时间:2018-03-15 17:18:13

标签: java android

我一直在关注下拉列表this的教程,在我的应用中添加自定义下拉微调器。我做的改变只是我添加了一个数字数组而不是一个字符串数组。但是这个数字没有出现在微调器上,只是数字旁边的图标出现了。此外,旋转器的尺寸增大,我无法弄清楚原因。

以下是应用程序上显示的内容(我仅针对“光照强度”自定义了微调器):

enter image description here

以下是相关文件的代码段。

AlertsFragment.java

public class AlertsFragment extends Fragment{

    private Switch switchBadge;
    int [] numbers={10,20,30,40,50,60,70, 80, 90, 100, 110, 120};
    int flags[] = {R.drawable.low, R.drawable.low, R.drawable.low, R.drawable.low,
            R.drawable.medium, R.drawable.medium, R.drawable.medium, R.drawable.medium,
            R.drawable.high, R.drawable.high, R.drawable.high, R.drawable.high};


    private AlertsFragment.OnFragmentInteractionListener listener;

  //omitted some fragment functions codes

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.fragment_alerts, container, false);

        switchBadge= (Switch)view.findViewById(R.id.BadgeSwitch);
        switchBadge.setChecked(false);
        switchBadge.setTextOn("On");
        switchBadge.setTextOff("Off");

        Spinner spin = (Spinner)view.findViewById(R.id.LDRspinner);
        //spin.setOnItemSelectedListener(this);

        CustomAdapter customAdapter=new CustomAdapter(getActivity(),flags,numbers);
        spin.setAdapter(customAdapter);

        return view;
    }

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {

    Context context;
    int level[];
    int[] numbers;
    LayoutInflater inflter;

    public CustomAdapter(Context applicationContext, int[] level, int[] numbers) {
        this.context = applicationContext;
        this.level = level;
        this.numbers = numbers;
        inflter = (LayoutInflater.from(applicationContext));
    }

    @Override
    public int getCount() {
        return level.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = inflter.inflate(R.layout.custom_spinner_items, null);
        ImageView icon = (ImageView) view.findViewById(R.id.imageView);
        TextView names = (TextView) view.findViewById(R.id.textView);
        icon.setImageResource(level[i]);
        names.setText(String.valueOf(numbers[i]));
        return view;
    }

}

custom_spinner_items.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:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="5dp"
        android:src="@drawable/high" /><!--Make sure image is present in Drawable folder-->

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="@dimen/activity_horizontal_margin"
        android:text="Demo"
        android:textColor="#000" />

</LinearLayout>

fragment_alerts.xml的一部分:

 <Spinner
            android:id="@+id/LDRspinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="30dp"
            android:layout_marginEnd="30dp"
            android:layout_marginRight="30dp"
            android:layout_weight="2"
            android:background="@android:drawable/btn_dropdown"
            android:spinnerMode="dropdown" />

在“温度”下方的应用程序中还有一个“湿度”微调器,但由于我自定义了Light Intensity微调器,它不再显示。

我很感激帮助修复这一切。

0 个答案:

没有答案