定制纺纱机的间距 [英] Customizing spinners' spacing

查看:218
本文介绍了定制纺纱机的间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

旋转器提供两种状态。第一个和默认状态(状态A )显示当前选择的值。第二个(状态B )在触摸微调显示时显示一个下拉菜单。





默认情况下,左填充添加到状态A中显示的项目, B.当显示当前选定的值(状态A ),但是当项目显示在下拉菜单(状态B )时,我想删除它。



由于使用在创建微调框时指定的布局中使用的 CheckedTextView 来设置填充,所以我的第一个尝试是传递给微调框构造器包含带有空填充的CheckedTextView的自定义布局。这样,左边的填充在状态A中消失,但也在状态B.然而,我的目标是保持状态A.



我的第二个尝试是自定义 android:dropDownSpinnerStyle 在我的主题定义。由于更改背景颜色为android:dropDownSpinnerStyle 仅更改状态A中的项目的背景颜色,我的想法是用负值覆盖marginLeft或paddingLeft。不幸的是,它没有任何效果。



鉴于负利润/填充似乎不被考虑,我尝试了相反的。首先,我使用了一个自定义项目布局(如我第一次尝试所解释的),以便在两个状态(A和B)上删除左填充。其次,我已经为属性定义了一个自定义样式android:dropDownListViewStyle 。不幸的是,使用最后一个属性的正边距值不起作用。因此,我已经设置了paddingLeft。它工作,并允许我获得状态B的左边距。然而,左侧空间也适用于背景触摸颜色(参见下图)。





我认为,如果我想让触摸背景颜色完全填充下拉菜单宽度,那么只能改变状态A的样式。欢迎任何想法,建议或示例。



以下是我第三次尝试的主题定义:

  ; style name =Theme.App.Baseparent =Theme.App> 
...
< item name =android:dropDownListViewStyle> @ style / Widget.Spinner.DropDown.ListView< / item>
< / style>

< style name =Widget.Spinner.DropDown.ListViewparent =Widget.AppCompat.ListView.DropDown>
< item name =android:paddingLeft> 16dp< / item>
< / style>


解决方案

状态A采用以下风格:

  //主题(Base)// Theme.AppCompat 
@android:style / Widget.TextView.SpinnerItem

// Holo& Holo Light
@android:style / Widget.Holo.TextView.SpinnerItem

这里玩的是 spinnerItemStyle



此外,提供的填充是 paddingLeft ,但 paddingStart - 支持LTR& RTL语言。同样, paddingEnd 被设置,而不是 paddingRight 。此信息适用于API> = 17。



如果您使用的是AppCompat,您仍然将覆盖 spinnerItemStyle 属性,但提供 paddingLeft paddingRight



示例:

 < style name =Theme.App.Baseparent =Theme.App> 
...
< item name =android:spinnerItemStyle> @ style / TextViewSpinnerItem< / item>
< / style>

< style name =TextViewSpinnerItemparent =@ android:style / Widget.TextView.SpinnerItem>
< item name =android:paddingLeft> 40dp< / item>
< item name =android:paddingRight> 40dp< / item>
< / style>

40dp 值用于测试是否设置这种风格甚至奏效。这应该只能填充状态A (具有40dp),留下状态B ,默认填充为 8DP 。一旦确认,您可以使其 0dp 或根据您的要求。



这是我得到的结果:





更新:



参考示例项目 - MainActivity

  spinner.setAdapter(ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item));

通过给适配器 android.R.layout.simple_spinner_item ,你正在告诉它使用 状态A和状态B的布局。这是一个问题,因为这个布局被定义的方式:

 < TextView xmlns:android =http://schemas.android.com/apk/res/android
android:id =@ android: id / text1
style =?android:attr / spinnerItemStyle
android:singleLine =true
android:layout_width =match_parent
android:layout_height =wrap_content
android:ellipsize =marquee
android:textAlignment =inherit/>

请注意样式应用于此的TextView 。此前,我建议您覆盖此属性。它工作。但是由于这种布局是用于这两种状态的,所以结果不是那么理想。



事实上,上面的声明(尽管目前没有做任何事情)更有希望:

  ArrayAdapter.createFromResource(this,
R.array.planets_array,android.R.layout.simple_spinner_item )
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

使用 setDropDownViewResource(int)将允许引入不同风格属性的可能性。在这种情况下,状态A将由 android.R.layout.simple_spinner_item 表示,状态B将使用 android.R.layout.simple_spinner_dropdown_item



我们来看看 android.R.layout.simple_spinner_dropdown_item

 < CheckedTextView xmlns:android =http://schemas.android.com/apk/res/android
android:id =@ android :id / text1
style =?android:attr / spinnerDropDownItemStyle
android:singleLine =true
android:layout_width =match_parent
android:layout_height = ?android:attr / dropdownListPreferredItemHeight
android:ellipsize =marquee/>

现在我们可以覆盖另一个属性 - spinnerDropDownItemStyle - 给B状态一个不同的外观。但是,我们不会。在Lollipop上, spinnerDropDownItemStyle 指向样式 Widget.Material.DropDownItem.Spinner 它设置 paddingX 8dp 。而且你说在B​​状态下你可以使用默认填充。



所以,这就是你需要的:

  //创建一个ArrayAdapter 
ArrayAdapter< CharSequence> mAdapter = ArrayAdapter.createFromResource(this,
R.array.planets_array,android.R.layout.simple_spinner_item);

//状态B
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

如果您还没有,请将其添加到值/ styles.xml中: / p>

 < style name =AppThemeparent =Theme.AppCompat.Light.DarkActionBar> 
< item name =android:spinnerItemStyle> @ style / TextViewSpinnerItem< / item>
< / style>

< style name =TextViewSpinnerItemparent =@ android:style / Widget.TextView.SpinnerItem>
< item name =android:paddingLeft> 0dp< / item>
< item name =android:paddingRight> 0dp< / item>
< / style>

您还应该创建 values-v21 / styles.xml 并添加:

 < style name =TextViewSpinnerItemparent =@ android:style / Widget.Material。 TextView.SpinnerItem> 
< item name =android:paddingLeft> 0dp< / item>
< item name =android:paddingRight> 0dp< / item>
< / style>


Spinners provide two states. The first and default state (state A) shows the currently selected value. The second (state B) shows a dropdown menu when the spinner display is touched.

By default a left padding is added to the items displayed in state A and B. I would like to remove it when the current selected value is displayed (state A) but to keep it when items are displayed in the dropdown menu (state B).

Since the padding is set with the CheckedTextView used in the layout that is specified when the spinner is created, my first try was to pass to the spinner constructor a custom layout that contains a CheckedTextView with empty padding. This way, the left padding disapears in state A but also in state B. However, my goal was to keep it for state A.

My second try was to customize android:dropDownSpinnerStyle in my theme definition. Since changing background color for android:dropDownSpinnerStyle changes the background color of the item in state A only, my thought was to override the marginLeft or paddingLeft with a negative value. Unfortunately, it has no effect.

Given that negative margin/padding seems not to be taken into account, I have tried the opposite. Firstly, I have used a custom item layout (as explained for my first try) in order to remove left padding on both states (A and B). Secondly, I have defined a custom style for property android:dropDownListViewStyle. Unfortunately, using a positive marginLeft value with the last property has no effect. Thus, I have set paddingLeft. It works and allows me to get the left spacing for state B only. However, the left space applies also to the background touch color (cf. image below).

I think that only the style for state A should be altered if I want to have the on touch background color that fully fills the dropdown menu width. Any idea, suggestion or example is welcomed.

Below is my theme definition for the third try:

<style name="Theme.App.Base" parent="Theme.App">
    ...
    <item name="android:dropDownListViewStyle">@style/Widget.Spinner.DropDown.ListView</item>
</style>

<style name="Widget.Spinner.DropDown.ListView" parent="Widget.AppCompat.ListView.DropDown">
    <item name="android:paddingLeft">16dp</item>
</style>

解决方案

State A takes on the following style:

// Theme (Base) // Theme.AppCompat
@android:style/Widget.TextView.SpinnerItem

// Holo & Holo Light
@android:style/Widget.Holo.TextView.SpinnerItem

The attribute at play here is spinnerItemStyle.

Moreover, the padding provided is not paddingLeft, but paddingStart - to support LTR & RTL languages. Similarly, paddingEnd is set instead of paddingRight. This info applies to API >=17.

If you are using AppCompat, you will still override the spinnerItemStyle attribute, but provide paddingLeft and paddingRight.

Example:

<style name="Theme.App.Base" parent="Theme.App">
...
    <item name="android:spinnerItemStyle">@style/TextViewSpinnerItem</item>
</style>

<style name="TextViewSpinnerItem" parent="@android:style/Widget.TextView.SpinnerItem">
    <item name="android:paddingLeft">40dp</item>
    <item name="android:paddingRight">40dp</item>
</style>

The 40dp value is for testing if setting this style even works. This should only pad State A(with 40dp), leaving State B with default padding of 8dp. Once confirmed, you can make it 0dp, or as per your requirement.

This is the result I get:

Update:

In reference to the sample project - MainActivity:

spinner.setAdapter(ArrayAdapter.createFromResource(this,
            R.array.planets_array, android.R.layout.simple_spinner_item));

By giving the adapter android.R.layout.simple_spinner_item, you are telling it to use the layout for both State A and State B. This is a problem because of the way this layout is defined:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:textAlignment="inherit"/>

Notice the style applied to this TextView. Earlier, I had suggested that you override this attribute. And it worked. But since this layout is used for both the states, the outcome is not as desired.

In fact, the statement right above (though not doing anything at the moment) is more promising:

ArrayAdapter.createFromResource(this,
            R.array.planets_array, android.R.layout.simple_spinner_item)
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

By using setDropDownViewResource(int), you will allow the possibility of introducing different style attributes. In this case, State A will be represented by android.R.layout.simple_spinner_item and State B will use android.R.layout.simple_spinner_dropdown_item.

Let's take a look at android.R.layout.simple_spinner_dropdown_item:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/dropdownListPreferredItemHeight"
    android:ellipsize="marquee"/>

Now we can override another attribute - spinnerDropDownItemStyle - and give State B a whole different look. But, we won't. On Lollipop, spinnerDropDownItemStyle points to style Widget.Material.DropDownItem.Spinner which sets the paddingX to 8dp. And you said that you're okay with the default padding in State B.

So, here's what you need:

// Create an ArrayAdapter
ArrayAdapter<CharSequence> mAdapter = ArrayAdapter.createFromResource(this,
            R.array.planets_array, android.R.layout.simple_spinner_item);

// State B
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

AND, if you don't have this already, add it to values/styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:spinnerItemStyle">@style/TextViewSpinnerItem</item>
</style>

<style name="TextViewSpinnerItem" parent="@android:style/Widget.TextView.SpinnerItem">
    <item name="android:paddingLeft">0dp</item>
    <item name="android:paddingRight">0dp</item>
</style>

You should also create values-v21/styles.xml and add:

<style name="TextViewSpinnerItem" parent="@android:style/Widget.Material.TextView.SpinnerItem">
    <item name="android:paddingLeft">0dp</item>
    <item name="android:paddingRight">0dp</item>
</style>

这篇关于定制纺纱机的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆