سؤال

By default if I create a Spinner in the graphical layout editor (using the Spinner Item preview layout, i.e android.R.layout.simple_spinner_item) the displayed text is

Item 1

Is there any way to change this preview text ?

هل كانت مفيدة؟

المحلول 3

Views have a function called "isEditMode()" that can be used to change the way items look in the Graphical Editor. This SO might help you out:

Custom Android Views in Eclipse Visual Editor

نصائح أخرى

For a spinner preview text in particular, use the tools:listitem attribute together with a layout:

<Spinner
 android:id="@+id/spinner1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 tools:listitem="@android:layout/simple_list_item_1" />

You can also set that preview in the visual editor by right-clicking on the Spinner and then selecting "Preview spinner layout". Anyway, it has to be a concrete layout, no simple text string.

So the best practice would be to set your dummy texts in the particular list item layout that you are going to use anyway (e.g. in your Adapter in Java code), and then directly preview that layout as described above.

First you need to create an appropriate preview layout. For example, you could put this in layout/preview.xml:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:text="NEW PREVIEW TEXT"
    android:ellipsize="marquee" />

Then you can right-click the Spinner in your actual layout and select Preview Spinner Layout > Choose Layout... Choose your layout from your project resources, and you should see your new preview.

You can also set the preview layout in XML with tools:listitem="@layout/preview"

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top