如何在 Spinner for android 中对齐文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20852561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:35:11  来源:igfitidea点击:

How to align text in Spinner for android?

androidspinner

提问by Aditi K

I want to align text in Spinner to the left but how can i achieve this require help

我想将 Spinner 中的文本向左对齐,但如何实现这一点需要帮助

here is my code & screen shot

这是我的代码和屏幕截图

 <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/roundshape" >

        <!-- Lable Area -->

        <TableRow
            android:id="@+id/tblRwspnLbl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"           
            android:gravity="center" 
            android:padding="1dip">

            <TextView
                android:id="@+id/lblCust"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="@string/lblCust"
                android:textSize="14sp"
                android:textStyle="bold" 
                android:gravity="left"/>

            <TextView
                android:id="@+id/lblPros"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="@string/lblPros"
                android:textSize="14sp"
                android:textStyle="bold" />
        </TableRow>

        <!-- Spinner Area -->

        <TableRow
            android:id="@+id/tblRwspn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="1dip"                
            android:gravity="center" >

            <Spinner
                android:id="@+id/spnAECust"
                android:layout_width="75dp"
                android:layout_height="35dp"
                android:fontFamily="verdana,arial,helvetica"
                android:hint="@string/SelectCust"
                android:textSize="14sp" 
                android:layout_gravity="left"/>

            <Spinner
                android:id="@+id/spnAEProspect"
                android:layout_width="75dp"
                android:layout_height="35dp"
                android:fontFamily="verdana,arial,helvetica"
                android:hint="@string/SelectProspect"
                android:textSize="14sp" />
        </TableRow>

        <!-- Text Area -->

Here is my Screen the red dot is the space which i want to remove so tht my all labels and spinners will come in same aligned line

这是我的屏幕,红点是我想要删除的空间,这样我的所有标签和微调器都会在同一条对齐线上

enter image description here

在此处输入图片说明

采纳答案by Aditi K

Thank you all for giving all possible answers here is solution i get

感谢大家提供所有可能的答案,这是我得到的解决方案

?android:layout_width="75dp"

  android:layout_height="35dp"

? android:layout_marginLeft="5dp"

  android:paddingLeft="5dp"

  android:gravity="left"

回答by M D

Just remove android:padding="1dip"from Spinner TableRow and try i am not sure just try

只需android:padding="1dip"从 Spinner TableRow 中删除并尝试我不确定只是尝试

Edit: You have to use a custom view here to give alignment to the Spinner values. Create a CustomView like thisanswer has created and add android:gravity' to the TextView asright`.

编辑:您必须在此处使用自定义视图来对齐 Spinner 值。创建一个像这个答案已经创建的android:gravity' to the TextView asCustomView并添加right`。

And set the CustomView to your adapter using

并使用将 CustomView 设置为您的适配器

adapter.setDropDownViewResource(android.R.layout.your_custom_created_view);

回答by Ketan Ahir

I think you should use custom ArrayAdapterwith TextViewand play with textview property as per your requirement.

我认为您应该根据您的要求使用 custom ArrayAdapterwithTextView并使用 textview 属性。

Example :

例子 :

MyAdapter.java

我的适配器

    public class MyAdapter extends ArrayAdapter {
Context context;
    public MyAdapter(Context context, int resource) {
        super(context, resource);
        this.context=context;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view=inflater.inflate(R.layout.mylayout, parent,false);
        TextView textView=(TextView)view.findViewById(R.id.text);

        return view;
    }
}

mylayout.xml

布局文件

 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <TextView
         android:id="@+id/text"
         android:layout_width="match-parent"
         android:layout_height="wrap_content"
         android:text="@string/lblPros"
         android:textSize="14sp"
         android:gravity="right"
         android:textStyle="bold" />
</LinearLayout>

回答by Vinit Vikash

Try this:-

尝试这个:-

use android:layout_span="1". if required then give left margin for the first row.

使用 android:layout_span="1"。如果需要,则为第一行提供左边距。

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/roundshape" >

    <!-- Lable Area -->

    <TableRow
        android:id="@+id/tblRwspnLbl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"           
        android:gravity="center" ">

        <TextView
            android:id="@+id/lblCust"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/lblCust"
            android:layout_span="1"
            android:textSize="14sp"
            android:textStyle="bold" 
            android:gravity="left"/>

        <TextView
            android:id="@+id/lblPros"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="1"
            android:text="@string/lblPros"
            android:textSize="14sp"
            android:textStyle="bold" />
    </TableRow>

    <!-- Spinner Area -->

    <TableRow
        android:id="@+id/tblRwspn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"             
        android:gravity="center" >

        <Spinner
            android:id="@+id/spnAECust"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="1"
            android:fontFamily="verdana,arial,helvetica"
            android:hint="@string/SelectCust"
            android:textSize="14sp" 
            android:layout_gravity="left"/>

        <Spinner
            android:id="@+id/spnAEProspect"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="1"
            android:fontFamily="verdana,arial,helvetica"
            android:hint="@string/SelectProspect"
            android:textSize="14sp" />
    </TableRow>

    <!-- Text Area -->

回答by hareesh J

This is sample code it may useful

这是可能有用的示例代码

<Spinner
                    android:id="@+id/spinner1"
                    style="?android:attr/spinnerStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginBottom="5dp"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="5dp"
                    android:background="@drawable/dropdown"
                    android:dropDownVerticalOffset="1dp"
                    android:focusable="false"
                    android:spinnerMode="dropdown" />

take one more layout for the text view following is the code for layout

为文本视图再做一个布局,下面是布局代码

spinner_text_layout.xml

spinner_text_layout.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text_for_spinner"
    style="?android:attr/spinnerItemStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawablePadding="10dp"
    android:drawableRight="@drawable/arrow"
    android:gravity="center"
    android:textColor="@android:color/white" >

</TextView>

now in the custom adapter these are the two methods

现在在自定义适配器中,这是两种方法

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.spinner_text_layout, null);
        }
        TextView textView = (TextView) convertView
                .findViewById(R.id.text_for_spinner);
        textView.setText((String) getItem(position));
        notifyDataSetChanged();
        return convertView;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.spinner_text_layout, null);
        }
        TextView textView = (TextView) convertView
                .findViewById(R.id.text_for_spinner);
        textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        textView.setText((String) getItem(position));
        textView.setTextColor(Color.BLACK);
        textView.setBackgroundResource(R.drawable.drop_down_selector);
        return convertView;
    }