微调框在自定义对话框中不显示任何文本

时间:2019-12-04 11:38:27

标签: android android-alertdialog android-spinner android-custom-view

我有一个带有自定义视图的微调器。当在片段中使用微调器时,效果很好,但将其移动到活动中时,其中不显示任何文本或项目。对话框中的其他项目效果很好,问题仅出在微调器中。我已经调试了几个小时的代码,但没有希望。

这是java代码:

   LayoutInflater layoutInflaterAndroid = LayoutInflater.from(MainActivity.this);
    final View view = layoutInflaterAndroid.inflate(R.layout.dialog_with_subject_spinner, null);

    final AlertDialog alertDialogBuilderUserInput = new AlertDialog.Builder(MainActivity.this)
            .setView(view)
            .setCancelable(false)
            .create();

    final EditText inputComment = view.findViewById(R.id.dialog_value);
    TextView dialogTitle = view.findViewById(R.id.dialog_title);
    Spinner spinner = view.findViewById(R.id.dialogSubjectsSpinner);
    dialogTitle.setText("إضافة منشور");
    inputComment.setHint("اكتب سؤالك هنا");

    ImageView closeIcon = view.findViewById(R.id.closeIcon);
    closeIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            alertDialogBuilderUserInput.dismiss();
        }
    });

    ArrayAdapter<String> subjectsAdapter=new ArrayAdapter<String>(MainActivity.this,R.layout.testactiv,R.array.preparatory_subjects_array_with_general_subjects_item){


        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {

            View v = null;
            v = super.getDropDownView(position, null, parent);
            // If this is the selected item position
            if (position == selectedItem) {
                v.setBackgroundColor(getResources().getColor(R.color.blue_white));

                TextView tv = (TextView) v.findViewById(R.id.textView);

                // Set the text color of spinner item
                tv.setTextColor(Color.WHITE);


            } else {
                // for other views
                v.setBackgroundColor(Color.WHITE);

            }
            return v;
        }
    };
    spinner.setAdapter(subjectsAdapter);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            selectedItem =  i;
            currentSubject = adapterView.getItemAtPosition(i).toString();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

这是微调器的xml代码:

<Spinner
    android:id="@+id/dialogSubjectsSpinner"
    android:layout_width="0dp"
    android:layout_marginTop="@dimen/_5sdp"
    android:layout_height="@dimen/_40sdp"
    android:layout_marginStart="@dimen/_16sdp"
    android:layout_marginEnd="@dimen/_16sdp"
    android:background="@drawable/gradient_spinner"
    android:ems="10"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/writelessonname"
    app:layout_constraintVertical_bias="0.0" />

这是drawable的代码:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item><layer-list>
    <item><shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        >
        <corners
            android:radius="@dimen/_10sdp"
            />
        <solid
            android:color="#FFFF"
            />


        <stroke
            android:width="2dp"
            android:color="@color/gray" />
        <padding
            android:left="@dimen/_15sdp"/>
    </shape></item>
    <item ><bitmap android:gravity="end|center"


        android:src="@drawable/spin" />
    </item>





</layer-list>


</item>

0 个答案:

没有答案