我正在尝试通过重新启动方向更改的活动来解决问题.
我有一个ActionBar带有下拉列表导航,每次旋转后,该列表的第一个元素被激活.保持fragment内容并不困难,但我不知道如何设置活动列表项.
这是以下定义ActionBar:
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ArrayAdapter<CharSequence> list = ArrayAdapter
.createFromResource(this, R.array.action_list, android.R.layout.simple_dropdown_item_1line);
list.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
getActionBar().setListNavigationCallbacks(list, this);
Run Code Online (Sandbox Code Playgroud)
这是我的解决方法:
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
if (!application.isRotated) {
application.activePosition = itemPosition;
application.activeId = itemId;
getFragmentManager().beginTransaction()
.replace(android.R.id.content, MyFragment.newInstance(itemPosition))
.commit();
} else {
application.isRotated = false;
this.onNavigationItemSelected(application.activePosition, application.activeId);
}
return true;
}
@Override
protected void onStop() {
super.onStop();
application.isRotated = true;
}
Run Code Online (Sandbox Code Playgroud)
我不确定这是最好的解决方案.
我找到的解决方案更改了微调器下拉图标,其中所有:
1.创建一个自定义drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/vector_drawable_ic_dropdown_black" android:state_focused="true" android:state_pressed="false" />
<item android:drawable="@drawable/vector_drawable_ic_dropdown_black" android:state_focused="true" android:state_pressed="true" />
<item android:drawable="@drawable/vector_drawable_ic_dropdown_black" android:state_focused="false" android:state_pressed="true" />
<item android:drawable="@drawable/vector_drawable_ic_dropdown_black" />
</selector>
Run Code Online (Sandbox Code Playgroud)
2.将drawable设置为微调器背景:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:background="@drawable/custom_spinner_icon"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:textColor="@color/textcolorprimary" />
Run Code Online (Sandbox Code Playgroud)
结果是:
正如您所看到的,这不是一个可接受的解决方案,因为图标需要右对齐而不是拉伸.
我该怎么做才能使图标不拉伸并对齐?
编辑
由于没有可行的解决方案,我想我必须指出我的问题.这就是我的Spinner使用标准主题的样子:
<Spinner
android:id="@+id/products_download_spinner_language"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:textColor="@color/textcolorprimary"
android:theme="@android:style/Theme.Holo.Light.DarkActionBar" />
Run Code Online (Sandbox Code Playgroud)
而我想要的一切(我猜这真的不多)正在改变箭头.我不希望显示右下角的箭头,我希望这个箭头垂直居中显示在右边:
到目前为止我尝试的每个解决方案:
根本没工作.他们有拉伸的图标或底线缺失或其他东西完全错误.我只想要另一支箭.
视图文本中最中心的最微调 - 当 - 选择 - 来自该下拉列表
我想将微调器的视图文本与中心对齐.我谷歌但没有找到任何东西,有人知道吗?任何帮助或建议表示赞赏
我正在尝试获取微调器的位置(数字),当选择在另一个Activity中使用它时,每次将根据所选项目显示不同的地图.当我运行应用程序时它崩溃了.这是第一个活动代码:
public class TestProjectActivity extends Activity {
public Spinner spinner1;
public Integer number;
private Button valideButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MySpinner();
valide_button();
}
public void MySpinner() {
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.num, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
// Object item = parentView.getItemAtPosition(position);
TestProjectActivity.this.number = spinner1
.getSelectedItemPosition() + 1;
}
public void onNothingSelected(AdapterView<?> arg0) {// do nothing
}
}); …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用一个看起来像微调器的文本框来激活日期选择器对话框.这可以在Google日历应用和ICS上的通讯录应用(生日)中完成.我是否需要使用微调器,如果是这样,我如何将其输入视图更改为日期选择器?或者如果没有,我如何获得文本视图以获得通常表示微调器的小三角形?
android android-datepicker android-spinner android-4.0-ice-cream-sandwich
我只是想知道是否可以简单地删除微调器的下拉箭头?我的旋转器在背景布局中有一个可绘制的箭头,但系统默认箭头出现在旋转器的右侧,我想摆脱它.
这是我的活动布局的微调器xml代码
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinnerSelectStaff"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:gravity="center"
android:dropDownSelector="@drawable/empty"/>
Run Code Online (Sandbox Code Playgroud)
我的自定义微调器布局如下所示:
<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:layout_margin="5dp"
android:gravity="center"
android:textSize="20sp"
android:background="@drawable/spinner_text_shape"
android:drawableRight="@drawable/ic_keyboard_arrow_down_black_24dp"
android:textColor="@color/primary_text" />
Run Code Online (Sandbox Code Playgroud)
谢谢!
android spinner android-layout android-spinner android-selector
如何在微调器中更改所选项的字体颜色?
我可以更改所选项目的背景颜色,下拉项目的颜色等,但不能更改所选项目的文本颜色...我该怎么做?
我的代码是:这是我正在使用的微调器 - :
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="32dip"
android:background="@drawable/mybg"
android:divider="@drawable/list_divider"
android:drawSelectorOnTop="true"
android:popupBackground="#D3D5D3"
android:prompt="@string/activityy_prompt"
/>
Run Code Online (Sandbox Code Playgroud)
这是mybg.xml
<!-- <item android:drawable="@drawable/blue" android:state_pressed="false"/> -->
<!-- <item android:drawable="@drawable/back11"/> -->
<item android:drawable="@drawable/greenyellow1" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/greenyellow1" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/greenyellow1" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@drawable/greenyellow1" android:state_selected="true"/>
<item android:drawable="@drawable/back11"/>
Run Code Online (Sandbox Code Playgroud)
使用这些我无法改变selecetd项目的文字颜色......
当我创建我的活动时,我设置了一个Spinner,为它分配一个监听器和一个初始值.我知道onItemSelected在应用程序初始化期间会自动调用回调.我觉得很奇怪的是,当设备旋转时会发生两次这种情况,这会导致我遇到一些问题,我不得不绕过它.如果微调器初始选择为零,则不会发生这种情况.我能够隔离问题,这是触发它的最简单的活动:
public class MainActivity extends Activity implements OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("Test","Activity onCreate");
setContentView(R.layout.activity_main);
((Spinner)findViewById(R.id.spinner1)).setSelection(2);
((Spinner)findViewById(R.id.spinner1)).setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> spin, View selview, int pos, long selId)
{
Log.i("Test","spin:"+spin+" sel:"+selview+" pos:"+pos+" selId:"+selId);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {}
}
Run Code Online (Sandbox Code Playgroud)
这是启动应用程序然后旋转设备时显示的logcat:
I/Test( 9881): spin:android.widget.Spinner@4052f508 sel:android.widget.TextView@40530b08 pos:2 selId:2
I/Test( 9881): Activity onCreate
I/Test( 9881): spin:android.widget.Spinner@40535d80 sel:android.widget.TextView@40538758 pos:2 selId:2
I/Test( 9881): spin:android.widget.Spinner@40535d80 sel:android.widget.TextView@40538758 pos:2 selId:2
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?我错过了什么吗?
在阅读了Google材料设计的新设计指南后,我没有找到任何在材料设计中设计/显示微调器(http://developer.android.com/design/building-blocks/spinners.html)的明确解决方案.
最接近的将是这样的:http://material-design.storage.googleapis.com/publish/v_1/quantumexternal/0Bx4BSt6jniD7anplVHR3QkdNUHc/components_menus_behavior2.png - 但我很确定,会有更好的解决方案.特别是如果它涉及旋转弹出窗口.即便如此,我在Android工作室中找不到任何微调器/菜单元素 - 所以我认为这将是自定义控件.
我找到的最好的例子是上面链接的菜单,它涵盖了微调元素的功能.
您会建议显示此类控件并处于材料设计范围内.
android spinner android-spinner drop-down-menu material-design