对齐选定的微调项
Aligning Selected Spinner Item
我正在尝试微调微调器中所选项目的位置。 不是 下拉列表项,因为我已经在我的适配器中有一个自定义视图,但特别是选定的项目。
如您在屏幕截图中所见,"Any" 是当前选中的项目。但它在容器内的对齐方式很奇怪,因为它必须容纳下拉列表中最长的字符串,即 "Dark Purple Burnt Sienna" (或其他)。我想将所选文本右对齐,以便 "Any" 位于下拉指示器旁边,而不是在中间。
我试图调整我的自定义微调项视图,但它对所选项目没有任何影响。
我也尝试过在 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"/>
你可以试试这个
android:autoSizeTextType="uniform"
简单的解决方案是为 微调器 TextView 布局 构建另一个 Custom view
,并在其中为您的 TextView
指定 gravity
.像 :
<!--spinner_layout.xml (in layout/)-->
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
/>
在初始化 spinner 的 ArrayAdapter
时使用它:
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.spinner_layout,
spinnerArray);
UI 输出:
我可以给你一个可行的方法。
在XML
1) 添加一个名为 the_current_color text view
的新文本视图
2) 将此文本视图对齐到微调器上方并对齐到箭头图标的开头
或者你想要的任何地方。
在你的代码中
1) 当您从微调器收听(在所选项目上)时,只需获取当前文本并将其设置在 (the_current_color) 文本视图中。
2) 并尝试通过将颜色更改为透明来隐藏微调器中的文本。
希望有用
编辑
我可能还会提出一些建议,也许它会改变一些事情
假设我们有一个使用此字符串资源文件填充其适配器的微调器
<string-array name="my_adapter">
<item>Blue </item>
<item>Red</item>
</string-array>
到目前为止,我们有一个微调器,它在微调器的箭头图标的最左侧显示(红色项目)和(蓝色项目)。
像那样创建另一个字符串资源
<string-array name="my_adapter_two">
<item> Blue </item>
<item> Red</item>
</string-array>
正如您现在看到的那样,添加的 space 会让人产生字符串在箭头图标旁边的错觉
您可以添加space适合您的需要,您可以在单击某个项目后切换文件,然后再切换回来。
内边距、边距和对齐方式 在涉及微调器时会让人感到困惑。我 运行 也遇到了类似的问题。但对我来说,我没有使用自定义视图和适配器,所以我设法使用样式来解决这个问题。
对于您来说,因为您已经在使用自定义视图和适配器,您可以将微调器的 background 设置为 null,这样下拉箭头将不可见。
android:background="@null"
然后在 TextView 中,您可以将 drawableRight 指定为下拉箭头。
android:drawableRight="@drawable/ic_spinner_drop_arrow_white_24dp"
图标可以是可绘制的下拉矢量。还要指定箭头和文本之间的特定边距,您应该能够使用 drawablePadding
希望这对您有所帮助。
您可以尝试以下自定义视图。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="right"
android:padding="15dp"
android:textAlignment="gravity"
android:textColor="@color/black"
android:textSize="10dp" />
您可以在getView(int position, View convertView, ViewGroup parent)
方法中调整所选项目的设置。如果你只是想设置重力和文本对齐方式,你应该将它设置为方法中的TextView
like
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView v = new TextView(mContext);
v.setText(mColors.get(position));
v.setGravity(Gravity.END); // OR v.setGravity(Gravity.RIGHT);
return v;
}
或者,如果您想进行进一步的定制,您可以简单地扩充自定义布局:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View row = null;
if (inflater != null) {
row = inflater.inflate(R.layout.list_item_view_color, parent, false);
TextView textView = row.findViewById(R.id.textview);
textView .setText(mColors.get(position));
}
return row;
}
其中 list_item_view_color.xml
是所选值的布局文件。
(注意:如果您想使用像 autoSizeTextType
这样的选项,您可以在 xml 文件中为 AppCompatTextView
使用带有 app
前缀的选项)
您可以通过编程方式为 getView 方法创建 TextView,因此不会设置 属性 对齐方式,简单的做法是按照您在 getDropdownView 上执行的方式对其进行膨胀,或者简单地设置实例化视图中的适当引力。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView v = new TextView(mContext);
v.setText(mColors.get(position));
v.setGravity(Gravity.RIGHT);
return v;
}
我正在尝试微调微调器中所选项目的位置。 不是 下拉列表项,因为我已经在我的适配器中有一个自定义视图,但特别是选定的项目。
如您在屏幕截图中所见,"Any" 是当前选中的项目。但它在容器内的对齐方式很奇怪,因为它必须容纳下拉列表中最长的字符串,即 "Dark Purple Burnt Sienna" (或其他)。我想将所选文本右对齐,以便 "Any" 位于下拉指示器旁边,而不是在中间。
我试图调整我的自定义微调项视图,但它对所选项目没有任何影响。
我也尝试过在 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"/>
你可以试试这个
android:autoSizeTextType="uniform"
简单的解决方案是为 微调器 TextView 布局 构建另一个 Custom view
,并在其中为您的 TextView
指定 gravity
.像 :
<!--spinner_layout.xml (in layout/)-->
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
/>
在初始化 spinner 的 ArrayAdapter
时使用它:
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.spinner_layout,
spinnerArray);
UI 输出:
我可以给你一个可行的方法。
在XML
1) 添加一个名为 the_current_color text view
的新文本视图2) 将此文本视图对齐到微调器上方并对齐到箭头图标的开头 或者你想要的任何地方。
在你的代码中
1) 当您从微调器收听(在所选项目上)时,只需获取当前文本并将其设置在 (the_current_color) 文本视图中。
2) 并尝试通过将颜色更改为透明来隐藏微调器中的文本。
希望有用
编辑
我可能还会提出一些建议,也许它会改变一些事情
假设我们有一个使用此字符串资源文件填充其适配器的微调器
<string-array name="my_adapter">
<item>Blue </item>
<item>Red</item>
</string-array>
到目前为止,我们有一个微调器,它在微调器的箭头图标的最左侧显示(红色项目)和(蓝色项目)。
像那样创建另一个字符串资源
<string-array name="my_adapter_two">
<item> Blue </item>
<item> Red</item>
</string-array>
正如您现在看到的那样,添加的 space 会让人产生字符串在箭头图标旁边的错觉
您可以添加space适合您的需要,您可以在单击某个项目后切换文件,然后再切换回来。
内边距、边距和对齐方式 在涉及微调器时会让人感到困惑。我 运行 也遇到了类似的问题。但对我来说,我没有使用自定义视图和适配器,所以我设法使用样式来解决这个问题。 对于您来说,因为您已经在使用自定义视图和适配器,您可以将微调器的 background 设置为 null,这样下拉箭头将不可见。
android:background="@null"
然后在 TextView 中,您可以将 drawableRight 指定为下拉箭头。
android:drawableRight="@drawable/ic_spinner_drop_arrow_white_24dp"
图标可以是可绘制的下拉矢量。还要指定箭头和文本之间的特定边距,您应该能够使用 drawablePadding
希望这对您有所帮助。
您可以尝试以下自定义视图。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="right"
android:padding="15dp"
android:textAlignment="gravity"
android:textColor="@color/black"
android:textSize="10dp" />
您可以在getView(int position, View convertView, ViewGroup parent)
方法中调整所选项目的设置。如果你只是想设置重力和文本对齐方式,你应该将它设置为方法中的TextView
like
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView v = new TextView(mContext);
v.setText(mColors.get(position));
v.setGravity(Gravity.END); // OR v.setGravity(Gravity.RIGHT);
return v;
}
或者,如果您想进行进一步的定制,您可以简单地扩充自定义布局:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View row = null;
if (inflater != null) {
row = inflater.inflate(R.layout.list_item_view_color, parent, false);
TextView textView = row.findViewById(R.id.textview);
textView .setText(mColors.get(position));
}
return row;
}
其中 list_item_view_color.xml
是所选值的布局文件。
(注意:如果您想使用像 autoSizeTextType
这样的选项,您可以在 xml 文件中为 AppCompatTextView
使用带有 app
前缀的选项)
您可以通过编程方式为 getView 方法创建 TextView,因此不会设置 属性 对齐方式,简单的做法是按照您在 getDropdownView 上执行的方式对其进行膨胀,或者简单地设置实例化视图中的适当引力。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView v = new TextView(mContext);
v.setText(mColors.get(position));
v.setGravity(Gravity.RIGHT);
return v;
}