为什么在加载数据和文本颜色后微调器大小会发生变化?
Why spinner size is changed after loading data and text color?
当我使用 Retrofit 从 Web 服务将数据加载到微调器时,它增加了微调器的高度并更改了文本颜色。问题是什么?我该如何解决?
红色标记的微调器是未加载任何数据的标准尺寸。
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_monthly_target_ad"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.nitolniloygroup.operating.view.activity.MonthlyTargetADActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:drawable/btn_dropdown"
android:textColor="@android:color/holo_blue_bright"
android:id="@+id/spinnerZone" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:drawable/btn_dropdown"
android:textColor="@android:color/holo_blue_bright"
android:id="@+id/spinnerSubZone" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:drawable/btn_dropdown"
android:textColor="@android:color/holo_blue_bright"
android:id="@+id/spinnerBranch" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:drawable/btn_dropdown"
android:textColor="@android:color/holo_blue_bright"
android:id="@+id/spinnerFieldOfficer" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:drawable/btn_dropdown"
android:textColor="@android:color/holo_blue_bright"
android:id="@+id/spinnerFieldOfficeraaa" />
<Button
android:text="Search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_default"
android:onClick="onClickSearch"
android:id="@+id/button4" />
</LinearLayout>
</RelativeLayout>
因为您将微调器设为高度 warp_content。
为了避免给微调器一个特定的高度,比如 80dp
您可以设置 Spinner 的最小高度:
android:minHeight="80dp"
要更改微调器大小:使用 padding
到 0dp
<Spinner
android:id="@+id/spinnerSubZone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:drawable/btn_dropdown"
android:minHeight="50dp"
android:padding="0dp" //add this
android:textColor="@android:color/holo_blue_bright" />
要更改文本颜色:覆盖微调器对象中的 setOnItemSelectedListener
方法
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.RED); //change color
((TextView) parent.getChildAt(0)).setTextSize(10); //change size
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
或者您可以使用自定义适配器 class 请参阅 here
当我使用 Retrofit 从 Web 服务将数据加载到微调器时,它增加了微调器的高度并更改了文本颜色。问题是什么?我该如何解决?
红色标记的微调器是未加载任何数据的标准尺寸。
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_monthly_target_ad"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.nitolniloygroup.operating.view.activity.MonthlyTargetADActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:drawable/btn_dropdown"
android:textColor="@android:color/holo_blue_bright"
android:id="@+id/spinnerZone" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:drawable/btn_dropdown"
android:textColor="@android:color/holo_blue_bright"
android:id="@+id/spinnerSubZone" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:drawable/btn_dropdown"
android:textColor="@android:color/holo_blue_bright"
android:id="@+id/spinnerBranch" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:drawable/btn_dropdown"
android:textColor="@android:color/holo_blue_bright"
android:id="@+id/spinnerFieldOfficer" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:drawable/btn_dropdown"
android:textColor="@android:color/holo_blue_bright"
android:id="@+id/spinnerFieldOfficeraaa" />
<Button
android:text="Search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_default"
android:onClick="onClickSearch"
android:id="@+id/button4" />
</LinearLayout>
</RelativeLayout>
因为您将微调器设为高度 warp_content。 为了避免给微调器一个特定的高度,比如 80dp
您可以设置 Spinner 的最小高度:
android:minHeight="80dp"
要更改微调器大小:使用 padding
到 0dp
<Spinner
android:id="@+id/spinnerSubZone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:drawable/btn_dropdown"
android:minHeight="50dp"
android:padding="0dp" //add this
android:textColor="@android:color/holo_blue_bright" />
要更改文本颜色:覆盖微调器对象中的 setOnItemSelectedListener
方法
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.RED); //change color
((TextView) parent.getChildAt(0)).setTextSize(10); //change size
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
或者您可以使用自定义适配器 class 请参阅 here