如何让SearchableSpinner选中项文本与提示文本对齐?
Hey there! Let's fix that text alignment issue with your SearchableSpinner. The problem is that the selected item's text doesn't match the hint's alignment—here are two straightforward solutions to get them looking consistent:
Solution 1: Set Gravity Directly in Layout
If your hint text uses a specific alignment (like center, start, or end), you can add the android:gravity attribute directly to each SearchableSpinner to force the selected item to match that alignment. For example, if you want center-aligned text (matching most default hint behaviors), update your layout like this:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="15dp" android:layout_marginLeft="15dp" android:orientation="vertical"> <com.toptoche.searchablespinnerlibrary.SearchableSpinner android:id="@+id/statespinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="2dp" app:hintText="Please Select your State" android:padding="4dp" android:layout_marginBottom="6dp" android:gravity="center"/> <!-- Add this line --> <com.toptoche.searchablespinnerlibrary.SearchableSpinner android:id="@+id/districtspinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" app:hintText="Please Select your District" android:padding="4dp" android:layout_marginBottom="6dp" android:gravity="center"/> <!-- Add this line --> <com.toptoche.searchablespinnerlibrary.SearchableSpinner android:id="@+id/assemblyspinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" app:hintText="Please Select your Assembly Constituency" android:layout_marginBottom="6dp" android:padding="4dp" android:gravity="center"/> <!-- Add this line --> <com.toptoche.searchablespinnerlibrary.SearchableSpinner android:id="@+id/wardspinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" app:hintText="Please Select your Ward" android:layout_marginBottom="6dp" android:padding="4dp" android:gravity="center"/> <!-- Add this line --> </LinearLayout>
Adjust android:gravity to start, end, or another value based on your desired alignment.
Solution 2: Custom Item Layout (More Flexible)
If the direct gravity setting doesn't work (some third-party libraries override default attributes), create a custom layout for the spinner items to ensure full control over alignment.
- Create a new layout file named
spinner_item.xmlin yourres/layoutfolder:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="4dp" android:gravity="center" <!-- Match your hint's alignment here --> android:textSize="16sp"/>
- Update your
SearchableSpinnerelements to use this custom layout:
<com.toptoche.searchablespinnerlibrary.SearchableSpinner android:id="@+id/statespinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="2dp" app:hintText="Please Select your State" android:padding="4dp" android:layout_marginBottom="6dp" android:spinnerItemLayout="@layout/spinner_item" <!-- Add this --> android:dropDownItemLayout="@layout/spinner_item"/> <!-- Add this for dropdown items too -->
Repeat this for all your SearchableSpinner instances. This ensures both the selected item and the dropdown options use the same alignment as your hint text.
Either of these methods should make your selected item text match the hint's alignment perfectly!
内容的提问来源于stack exchange,提问作者Ashish

