我做了一些微调器,微调器的长度(宽度)比某些物品的名称短。
例如,选择一个项目后。
[South K...▼]
但是我想要的是这个
[South Kore▼]
(但不是[South Kore▼a]
也不是重叠的。)
似乎Layout(XML)中的Spinner没有像ellipsize这样的任何属性。而且我无法在线找到任何信息。
有什么办法可以解决这个问题?
我的资源在这里:
1. Country Name Array in [strings.xml]
<string-array name="countries_array">
<item>Argentina</item>
<item>Australia</item>
<item>Austria</item>
<item>Bangladesh</item>
<item>Belgium</item>
<item>Brazil</item>
<item>Bulgaria</item>
<item>Cambodia</item>
<item>Canada</item>
<item>Chile</item>
<item>China</item>
<item>Colombia</item>
<item>Czech Republic</item>
<item>Denmark</item>
<item>Egypt</item>
<item>Estonia</item>
<item>Fiji</item>
<item>Finland</item>
<item>France</item>
<item>Germany</item>
<item>Greece</item>
<item>Guam</item>
<item>Hong Kong</item>
<item>Hungary</item>
<item>Iceland</item>
<item>India</item>
<item>Indonesia</item>
<item>Iran</item>
<item>Iraq</item>
<item>Ireland</item>
<item>Israel</item>
<item>Italy</item>
<item>Jamaica</item>
<item>Japan</item>
<item>Kazakhstan</item>
<item>Laos</item>
<item>Luxembourg</item>
<item>Macau</item>
<item>Malaysia</item>
<item>Mexico</item>
<item>Monaco</item>
<item>Mongolia</item>
<item>Morocco</item>
<item>Mozambique</item>
<item>Myanmar</item>
<item>Nepal</item>
<item>Netherlands</item>
<item>New Zealand</item>
<item>Nigeria</item>
<item>North Korea</item>
<item>Norway</item>
<item>Pakistan</item>
<item>Peru</item>
<item>Philippines</item>
<item>Poland</item>
<item>Portugal</item>
<item>Romania</item>
<item>Russia</item>
<item>Senegal</item>
<item>Singapore</item>
<item>South Africa</item>
<item>South Korea</item>
<item>Spain</item>
<item>Sweden</item>
<item>Switzerland</item>
<item>Taiwan</item>
<item>Thailand</item>
<item>Tunisia</item>
<item>Turkey</item>
<item>Ukraine</item>
<item>United Arab Emirates</item>
<item>United Kingdom</item>
<item>United States</item>
<item>Uzbekistan</item>
<item>Vietnam</item>
</string-array>
2. Spinner in the Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/themeColor"
tools:context="org.koreanlab.origol.activity.NavigationActivity">
<LinearLayout
android:id="@+id/searchbar_linearlayout_searchfragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:orientation="horizontal"
android:weightSum="10"
android:background="@color/white"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center_vertical"
android:weightSum="10">
<Spinner
android:id="@+id/category_spinner_searchfragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
>
</Spinner>
<Spinner
android:id="@+id/subcategory_spinner_searchfragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
>
</Spinner>
<EditText
android:id="@+id/iamlookingfor_edittext_searchfragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="@string/iamlookingfor"
android:inputType="text"
android:maxLength="30"
android:maxLines="1" />
<Spinner
android:id="@+id/country_spinner_searchfragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:spinnerMode="dropdown"
android:prompt="@string/country_spinner_title"
>
</Spinner>
<Spinner
android:id="@+id/city_spinner_searchfragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:spinnerMode="dropdown"
android:prompt="@string/city_spinner_title">
</Spinner>
</LinearLayout>
</LinearLayout>
<ListView
android:id="@+id/article_listview_searchfragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/searchbar_linearlayout_searchfragment"
android:scrollbarStyle="outsideOverlay" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/writebutton_searchfragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:tint="@color/white"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
app:backgroundTint="@color/blueMiddle"
app:srcCompat="@drawable/baseline_brush_black_48" />
</RelativeLayout>
3. Java
ArrayAdapter<String> categoryAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.categories_array));
ArrayAdapter<String> countryAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.countries_array));
categorySP.setOnItemSelectedListener(this);
countrySP.setOnItemSelectedListener(this);
categoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
categorySP.setAdapter(categoryAdapter);
countrySP.setAdapter(countryAdapter);
答案 0 :(得分:3)
为Spinner提供自定义布局:
ArrayAdapter adapter= new ArrayAdapter(mOwnerActivity, R.layout.simple_list_item_1, new ArrayList<>());
布局:R.layout.simple_list_item_1->
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" />
为转盘设置适配器:
spinner.setAdapter(adapter);
希望它会对您有所帮助。
答案 1 :(得分:1)
您没有椭圆形
使用:
android:ellipsize = "none"
答案 2 :(得分:1)
创建适配器时,需要输入三个参数。
Context
,Layout
,ArrayList
因此,如果您更改Layout
(自定义)。您可以解决此问题。
这应该改变:
ArrayAdapter<String> categoryAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.categories_array));
收件人:
ArrayAdapter<String> categoryAdapter = new ArrayAdapter<>(getContext(), R.layout.custom_spinner_item, getResources().getStringArray(R.array.categories_array));
注意:它不是android.R.layout.xxx。但是R.layout.xxx没有android。
并创建这样的布局(在布局下):
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/spinner_margin"
android:textColor="@color/black"
android:maxLines="1"
/>
您可以根据自己的喜好更改@dimen
和@color
。
我所做的是:
colors.xml
<color name="black">#000</color>
dimen.xml
<dimen name="spinner_margin">0dp</dimen>