在 android 中的微调器中设置视图文本居中对齐

发布于 2024-12-05 21:39:31 字数 163 浏览 1 评论 0原文

view-text-in-the-center-of-the-spinner-when-select-from-the-drop-down-list

我想将微调器的视图文本对齐到中心。我google了一下,没有找到任何东西,有人知道这个吗?任何帮助或建议表示赞赏

view-text-in-the-center-of-the-spinner-when-select-from-the-drop-down-list

I want to align the view text of spinner to center. I google it but didn't find anything, does anybody knows about this? any help or suggestion are appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

眼眸 2024-12-12 21:39:31

像这样为您的微调器创建一个适配器,

ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.my_spinner_style,array_of_values) {

    public View getView(int position, View convertView,ViewGroup parent) {

        View v = super.getView(position, convertView, parent);

        ((TextView) v).setTextSize(16);

        return v;

    }

    public View getDropDownView(int position, View convertView,ViewGroup parent) {

        View v = super.getDropDownView(position, convertView,parent);

        ((TextView) v).setGravity(Gravity.CENTER);

        return v;

    }

};

现在您的布局R.layout.my_spinner_style

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />

现在将此适配器设置为您的微调器,

spinner.setAdapter(adapter);

Create a adapter for your spinner like this,

ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.my_spinner_style,array_of_values) {

    public View getView(int position, View convertView,ViewGroup parent) {

        View v = super.getView(position, convertView, parent);

        ((TextView) v).setTextSize(16);

        return v;

    }

    public View getDropDownView(int position, View convertView,ViewGroup parent) {

        View v = super.getDropDownView(position, convertView,parent);

        ((TextView) v).setGravity(Gravity.CENTER);

        return v;

    }

};

Now your layout R.layout.my_spinner_style,

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />

Now set this adapter to your spinner,

spinner.setAdapter(adapter);
堇色安年 2024-12-12 21:39:31

您需要为微调项目设置自己的布局。

SpinnerAdapter adap = new ArrayAdapter<String>(this, R.layout.spinner_item, new String[]{"A", "B", "C"});
spriner.setAdapter(adap);

其中 R.layout.spinner_item 是包含内容的布局:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" 
    android:gravity="center" 
    android:textColor="#000000" 
    android:text="Sample Text" 
    android:paddingBottom="5dp" 
    android:paddingTop="5dp"></TextView>

You need to set your own layout for spinner item.

SpinnerAdapter adap = new ArrayAdapter<String>(this, R.layout.spinner_item, new String[]{"A", "B", "C"});
spriner.setAdapter(adap);

Where R.layout.spinner_item is a layout with content:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" 
    android:gravity="center" 
    android:textColor="#000000" 
    android:text="Sample Text" 
    android:paddingBottom="5dp" 
    android:paddingTop="5dp"></TextView>
梨涡 2024-12-12 21:39:31

这很简单。只需在您的 Java 类中创建一个名为 spinner_item.xml 且内容为

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#111111"
android:padding="10dp"
android:textAlignment="center"
/>

Now 的 xml 文件,而不是添加 Android 的默认视图资源将您的自定义视图资源添加为

ArrayAdapter<String> adapter= new ArrayAdapter<String>(context, R.layout.spinner_item, myList);
 adapter.setDropDownViewResource(R.layout.spinner_item);

textAlignment 行即可解决问题。

This is very simple. Just create a xml file named as, for example, spinner_item.xml with content as

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#111111"
android:padding="10dp"
android:textAlignment="center"
/>

Now in your Java class, instead of adding Android's default view resource add your custom view resource as

ArrayAdapter<String> adapter= new ArrayAdapter<String>(context, R.layout.spinner_item, myList);
 adapter.setDropDownViewResource(R.layout.spinner_item);

The textAlignment line will do the trick.

梦罢 2024-12-12 21:39:31

textAlignment设置为居中会将文本置于居中。

android:textAlignment="center"

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/spinner_areas"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:visibility="visible"
            android:gravity="center"
            android:textAlignment="center"/>

Setting textAlignment to center will place the text in center.

android:textAlignment="center"

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/spinner_areas"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:visibility="visible"
            android:gravity="center"
            android:textAlignment="center"/>
沫离伤花 2024-12-12 21:39:31

这无需更改布局即可工作

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
               ((TextView) adapterView.getChildAt(0)).setGravity(Gravity.CENTER);
        }
 });

This works without layout changes

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
               ((TextView) adapterView.getChildAt(0)).setGravity(Gravity.CENTER);
        }
 });
背叛残局 2024-12-12 21:39:31

在 Spinner 中添加这一行

android:textAlignment="center"

瞧!

Add this line in Spinner

android:textAlignment="center"

Voila!

相守太难 2024-12-12 21:39:31

游戏迟到了,但我想为未来的用户添加这个,只是因为我发现它是解决我的问题的简单解决方案。它允许我将简单文本居中,而无需创建自定义布局。我这样做的方法是使用 padding

<Spinner
    android:id="@+id/example_spinner"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="2dp"
    android:paddingBottom="2dp" />

我成功地使用这种方法将文本在微调器中居中。这可能只适用于简单的情况,但您可能希望对更复杂的情况使用自定义布局。这只是为了保留您的 xml 文件,并避免必须使用自定义布局来居中简单的微调器。

Late to the game, but I wanted to add this for future users just because I found it a simple solution for my problem. It allowed me to center simple text without creating a custom layout. The way I did this was using padding

<Spinner
    android:id="@+id/example_spinner"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="2dp"
    android:paddingBottom="2dp" />

I was successful in centering my text within my spinner using this method. This will likely only work for simple cases, but you would probably want to use a custom layout for more complex cases anyway. This is just to keep your xml files down and avoid having to use a custom layout just to center a simple spinner.

橘和柠 2024-12-12 21:39:31

就这么简单!

<Spinner
 android:textAlignment="center"
/>

Simple as this!

<Spinner
 android:textAlignment="center"
/>
も星光 2024-12-12 21:39:31

我发现编辑主题比创建适配器稍微容易一些。在我的fragment_first.xml 中,我有我的微调器,

<Spinner
    android:theme="@style/mySpinnerStyle"
    android:id="@+id/spnnr"
    android:layout_width="350dp"
    android:layout_height="75dp"
    android:layout_marginBottom="32dp"
    android:spinnerMode="dropdown"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />

然后我在值中创建了一个新的@style 资源(Kotlin 推荐)。

<style name="mySpinnerStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
    <item name="android:textSize">@dimen/ssp</item>
    <item name="android:textAlignment">center</item>

</style>

<dimen name="ssp">24sp</dimen>

在这里向法里德指点迷津。 https://stackoverflow.com/a/64193198/15078702

I found it slightly easier to edit the theme than to create an adapter. In my fragment_first.xml I have my spinner

<Spinner
    android:theme="@style/mySpinnerStyle"
    android:id="@+id/spnnr"
    android:layout_width="350dp"
    android:layout_height="75dp"
    android:layout_marginBottom="32dp"
    android:spinnerMode="dropdown"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />

I then created a new @style resource in values (recommended by Kotlin).

<style name="mySpinnerStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
    <item name="android:textSize">@dimen/ssp</item>
    <item name="android:textAlignment">center</item>

</style>

<dimen name="ssp">24sp</dimen>

points to Farid here for pointing me along. https://stackoverflow.com/a/64193198/15078702

夏末染殇 2024-12-12 21:39:31

让我完整回答一下。在 Spinner 中腾出空间也很重要。

错误:

<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

正确:

<Spinner
android:id="@+id/spinner"
android:layout_width="100dp"
android:layout_height="wrap_content"
/>

Let me complete answer. It is also important to make space in Spinner.

Wrong:

<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

Right:

<Spinner
android:id="@+id/spinner"
android:layout_width="100dp"
android:layout_height="wrap_content"
/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文