当前位置: 首页 > 知识库问答 >
问题:

对齐选定的旋转器项

蔡弘扬
2023-03-14

我正在试着对所选项目在旋转器中的定位做一个轻微的调整。不是下拉列表项,因为我在适配器中已经有了一个自定义视图,而是特定的选定项。

正如你在截图中看到的,“任何”是当前选择的项。但它在容器中的对齐很奇怪,因为它必须容纳下拉列表中最长的字符串,即“Dark Purple Burnt Sienna”(或其他)。我想将选定的文本向右对齐,这样“任何”就在下拉指示器旁边,而不是中间的出路。

我尝试对我的自定义旋转项视图进行调整,但它对选定的项没有任何影响。

<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"/>

共有1个答案

任小云
2023-03-14

您可以在GetView(int position,View convertView,ViewGroup parent)方法中调整选定项的设置。如果您只想设置重力和文本对齐方式,则应该将其设置为方法中的textview

@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等选项,可以将它们与app前缀一起用于xml文件中的AppCompattextView

 类似资料:
  • 我有一个图像组件,我想在其中旋转源: 我从代码中设置了ImageTarget的源代码。 在转换(RenderTransformorMorigin和RotateTransform)之前,我的窗口是这样的: 但在旋转之后: 所以,正如你所看到的,宽度和高度都发生了变化。 所以我的问题是: 为什么大小发生了变化? 如何将旋转后的图像对准左上角(与之前相同)? 谢谢 编辑:大小没有改变,我用不同的大小拍摄

  • 我正在创建一个侧边栏导航面板,其中一些链接是持有的,我想要他们显示侧身。下面是当前的导航面板: 分解一下: 菜单图标 下面是图标的HTML 它是CSS: HTML: CSS: 当我将添加到类时,结果如下所示: 我怎样才能旋转这些并且仍然使它们居中?谢谢!

  • 出于某种原因,我的旋转器中选定项的文本颜色为白色。以下是活动的代码: 下面是simple_spinner_item的XML: 下拉项显示为红色,但不显示所选项。我试图将XML中的textColor更改为selector:并创建了这个选择器: 但是所选项目的文本颜色仍然是白色。所以我尝试在OnItemSelectedListener中这样做: 还是白色的... 为什么会发生这种情况,我如何修复它?谢

  • 检查下图的x轴。如何将标签向左移动一点,使其与各自的刻度对齐? 我正在使用以下方法旋转标签: 但是,正如您所看到的,旋转集中在文本标签的中间。这让他们看起来像是右移了。 我试着用这个来代替: ... 但它没有实现我的愿望。而似乎是参数所允许的唯一值。

  • 我在警报对话框中有一个旋转器。我想减少旋转项之间的填充,因此我实现了以下内容: spinner_row.xml 活动代码包含以下内容: 按照Farrukh的建议,我尝试了他的代码,结果如下。

  • 编辑:选定项目的蓝色文本。