简体   繁体   中英

how do I give my spinner same width as my EditText?

I have my EditText width set to wrap_content

<EditText
            android:id="@+id/reg_password_retype"
            android:ems="10"
            android:hint="@string/passwordRetype"
            android:inputType="textPassword"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"/>

and below it I have my spinner as below:

   <Spinner
            android:id="@+id/states_options"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/send"
            android:popupBackground="@android:color/transparent"/>

I came from iOS development and there we have something called "equal width", is there a way on android that enables me to set the width of my spinner exactly the same as my edit text?

Like setting "Trailing & Leading Space" in iOS, in Android set same "Layout Margin Left and Right" for both EditText and Spinner. It might solve the issue.

You can wrap it around a LinearLayout and match it there for example like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/reg_password_retype"
            android:ems="10"
            android:hint="@string/passwordRetype"
            android:inputType="textPassword"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"/>

        <Spinner
            android:id="@+id/states_options"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/send"
            android:popupBackground="@android:color/transparent"/>
    </LinearLayout>


</LinearLayout>

It depends on your parent width, you can achieve the thing like this. Change the spinner width to match_parent. Hope this will solve the issue.

   <LinearLayout
      android:layout_width="wrap_content"
        android:layout_height="match_parent"
         android:orientation="vertical">
 <EditText
        android:id="@+id/reg_password_retype"
        android:ems="10"
        android:hint="@string/passwordRetype"
        android:inputType="textPassword"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>

        <Spinner
        android:id="@+id/states_options"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/send"
        android:popupBackground="@android:color/transparent"/>

Through code do this -

findView first

    EditText edt=(EditText)findviewById(R.id.reg_password_retype);
    Spinner spinner (Spinner)findViewById(R.id.states_options);
   getWindow().getDecorView().getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
          @Override
          public void onGlobalLayout() {
            LayoutParam lp=spinner.getlayoutParam();
           lp.width=edt.getWidth();
    sippner.setLayoutParam(lp);
            getWindow().getDecorView()
.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
          }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM