我的 EditText
宽度设置为 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="match_parent"
android:background="@drawable/send"
android:popupBackground="@android:color/transparent"/>
我来自iOS开发,那里有一些叫做"相等宽度"的东西,是否有一种方法可以使我能够设置旋转器的宽度与我的编辑文本完全相同?
喜欢设置iOS中的"尾随&amp; tearp; prade Space",在Android设置了相同的Edittext和Spinner的"左右布局"。它可能会解决问题。
您可以将其包裹在LinearLayout
上,并在此匹配,例如这样
<?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>
这取决于您的父宽度,您可以实现这样的目标。将旋转器宽度更改为match_parent。希望这能解决这个问题。
<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"/>
通过代码执行此操作 -
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);
}
});