【问题标题】:Customize the spinner in Android is giving space issue在 Android 中自定义微调器会产生空间问题
【发布时间】:2015-05-27 21:10:07
【问题描述】:

我已经自定义了微调器的背景。我已经删除了背景并在旁边显示了一个微调器向下箭头。但是如果文本没有正确对齐。较大长度的文本显示在完整区域中,但对于较小的文本,文本和箭头会出现间隙。我使用了以下代码:

<RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/txtSelectVehins"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginTop="30dp"
                android:text="Select Vehicle"
                android:textColor="#F8F1F1"
                android:textStyle="bold" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:layout_toRightOf="@id/txtSelectVehins" >

                <Spinner
                    android:id="@+id/sp_veh_insrenew"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:background="@null"
                    android:drawSelectorOnTop="true"
                    android:minHeight="0dp"
                    android:spinnerMode="dropdown"
                    android:textSize="16sp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:src="@drawable/dropdownarrow" />
            </RelativeLayout>
        </RelativeLayout>

我想要正确对齐的整个对齐方式。我已在此处附上屏幕截图:

请帮我解决这个问题。

这是我的微调器适配器:

 editStateAdapter = new ArrayAdapter<String>(EditStateCity.this, R.layout.spinner_item, Constants.stateList);
                  editStateAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
                  mSpEditState.setAdapter(editStateAdapter);

这里是微调器文本的自定义布局:

    <?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:textSize="13sp"
    android:textColor="#FFFFFF" 
    android:textStyle="bold"  
    android:paddingRight="20dip" 
    />

谢谢。

【问题讨论】:

  • 您是否尝试将重力设置为正确?
  • 为每个组件以及相对布局尝试了重力。没用。
  • 你是如何实现适配器的?如果要更改文本对齐方式,则不能使用默认微调器项目布局,您需要自定义。
  • 我已经添加了我在适配器上使用的自定义布局。你可以看到我添加代码的问题。
  • 在 TextView 中为 R.layout.spinner_item 和 R.layout.simple_spinner_dropdown_item 添加 android:gravity="right" 应该可以得到你想要的效果。

标签: android spinner android-relativelayout android-spinner


【解决方案1】:

微调器中的视图是从适配器获得的,因此如果您想更改其上的任何内容,包括文本对齐方式,您将需要编辑用作下拉资源的布局。

因此,获取您的 R.layout.spinner_itemR.layout.simple_spinner_dropdown_item 资源并更新它们,使它们包含的 TextView 的文本向右对齐。

<TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:textSize="13sp"
    android:textColor="#FFFFFF" 
    android:textStyle="bold"  
    android:paddingRight="20dip"
    android:gravity="right" />

【讨论】:

    猜你喜欢
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    相关资源
    最近更新 更多