【发布时间】:2017-10-27 14:41:13
【问题描述】:
我正在尝试对选定项目在微调器中的位置进行微调。 不是下拉列表项,因为我的适配器中已经有一个自定义视图,而是专门选择的项。
正如您在屏幕截图中看到的,“Any”是当前选中的项目。但它在容器内奇怪地对齐,因为它必须容纳下拉列表中最长的字符串,即“Dark Purple Burnt Sienna”(或其他)。我想将选定的文本向右对齐,以便“任何”在下拉指示器旁边,而不是在中间。
我已尝试调整我的自定义微调器项目视图,但它对所选项目没有任何影响。
我还尝试在 Spinner 本身上设置重力和文本对齐方式,但没有任何效果。
这是图片和xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/default_black"
android:layout_centerVertical="true"
android:text="Color" />
<Spinner
android:id="@+id/spn_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
编辑:这是我的适配器:
public class ColorsAdapter<T> implements SpinnerAdapter {
ArrayList<String> mColors;
ArrayList<Integer> mValues;
Context mContext;
public ColorsAdapter(ArrayList<String> colors, ArrayList<Integer> values,
Context context) {
mContext = context;
mColors = colors;
mValues = values;
}
@Override
public int getCount() {
return mColors.size();
}
@Override
public Object getItem(int position) {
return mColors.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return R.layout.list_item_color;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView v = new TextView(mContext);
v.setText(mColors.get(position));
return v;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE );
View row = inflater.inflate(getItemViewType(position), parent, false);
TextView tvName = (TextView)row.findViewById(R.id.tv_name);
tvName.setText(mColors.get(position));
row.setTag(mValues.get(position));
return row;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isEmpty() {
return false;
}
}
这是列表项的 XML:
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="right"/>
【问题讨论】: