在 android 中的微调器中设置视图文本居中对齐
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
像这样为您的微调器创建一个适配器,
现在您的布局R.layout.my_spinner_style,
现在将此适配器设置为您的微调器,
Create a adapter for your spinner like this,
Now your layout R.layout.my_spinner_style,
Now set this adapter to your spinner,
您需要为微调项目设置自己的布局。
其中 R.layout.spinner_item 是包含内容的布局:
You need to set your own layout for spinner item.
Where R.layout.spinner_item is a layout with content:
这很简单。只需在您的
Java
类中创建一个名为spinner_item.xml
且内容为Now 的
xml
文件,而不是添加 Android 的默认视图资源将您的自定义视图资源添加为textAlignment 行即可解决问题。
This is very simple. Just create a
xml
file named as, for example,spinner_item.xml
with content asNow in your
Java
class, instead of adding Android's default view resource add your custom view resource asThe textAlignment line will do the trick.
将textAlignment设置为居中会将文本置于居中。
android:textAlignment="center"
Setting textAlignment to center will place the text in center.
android:textAlignment="center"
这无需更改布局即可工作
This works without layout changes
在 Spinner 中添加这一行
瞧!
Add this line in Spinner
Voila!
游戏迟到了,但我想为未来的用户添加这个,只是因为我发现它是解决我的问题的简单解决方案。它允许我将简单文本居中,而无需创建自定义布局。我这样做的方法是使用
padding
我成功地使用这种方法将文本在微调器中居中。这可能只适用于简单的情况,但您可能希望对更复杂的情况使用自定义布局。这只是为了保留您的 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
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.
就这么简单!
Simple as this!
我发现编辑主题比创建适配器稍微容易一些。在我的fragment_first.xml 中,我有我的微调器,
然后我在值中创建了一个新的@style 资源(Kotlin 推荐)。
在这里向法里德指点迷津。 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
I then created a new @style resource in values (recommended by Kotlin).
points to Farid here for pointing me along. https://stackoverflow.com/a/64193198/15078702
让我完整回答一下。在 Spinner 中腾出空间也很重要。
错误:
正确:
Let me complete answer. It is also important to make space in Spinner.
Wrong:
Right: