Android Spinner Initial Padding

时间:2015-10-30 22:02:34

标签: android

I am creating an Android app that consists mainly of a form. This form needs to have EditText and Spinners side-by-side (for this I chose a LinearLayout). My final goal is to have the text in the Spinner align vertically with the text in the EditText; however, I also want to have padding on the options in the expanded Spinner. This padding messes up the Spinner alignment as it appears in the form.

Here is my code for a custom Spinner list item:

<TextView
android:id="@+id/transportation_spinner_item_textView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="@android:color/black"
android:textSize="18dp"/>

Here is my code for the main form:

<LinearLayout
    android:id="@+id/linearLayout6"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/divider3"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="100"
        android:orientation="vertical">

        <TextView
            android:id="@+id/ip_minutes_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/ip_minutes"
            android:layout_marginTop="4dp"
            android:textSize="12sp"/>

        <Spinner
            android:id="@+id/ip_minutes_spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:spinnerMode="dialog"
            android:gravity="bottom"/>
    </RelativeLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/ip_charge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="100"
        android:layout_gravity="bottom">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="100"
            android:editable="false"
            android:hint="@string/ip_charge"/>
    </android.support.design.widget.TextInputLayout>
</LinearLayout>

And here is an image of how it looks: Image of alignment with above code

I have been Googling and trying to adjust the padding/margins of different views for hours, so if anyone has any suggestions, that would be great. Thanks!

1 个答案:

答案 0 :(得分:0)

I'm not sure I fully understand, when you say,

This padding messes up the Spinner alignment as it appears in the form.

Do you mean the DropDownView? that shows when the Spinner is clicked?

If so, simply create a separate layout for the DropDownView.

In your ArrayAdapter you will have the following methods,

public View getDropDownView(int position, View cnvtView, ViewGroup prnt)

public View getView(int pos, View cnvtView, ViewGroup prnt)

Just have them return two different Views, e.g

@Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
    return getCustomDropDownView(position, cnvtView, prnt);
}
@Override
public View getView(int pos, View cnvtView, ViewGroup prnt) {
    return getCustomView(pos, cnvtView, prnt);
}

and

public View getCustomView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false);

    TextView main_text = (TextView) mySpinner.findViewById(R.id.spinner_text);
    main_text.setText(spinnerValues[position]);

    return mySpinner;
}

public View getCustomDropDownView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    View mySpinner = inflater.inflate(R.layout.custom_dropdown_spinner, parent, false);

    TextView main_text = (TextView) mySpinner.findViewById(R.id.spinner_text);
    main_text.setText(spinnerValues[position]);

    return mySpinner;
}