您的位置:首页 > 移动开发 > Android开发

Android 自定义spinner文字颜色 和 显示样式

2015-08-31 11:57 603 查看
      项目中界面,有几个数值不允许用户输入,只能在下拉列表中选择项目,一开始想过自定义dialog或者popupwindow,但是会额外增加很多代码,考虑到现在的工程代码量已经很多了,所以想到了使用google已经开发好的组件spinner组件,这是一个非常好用的系统下拉选项组件,具体的用法我就不多说了,有很多已经总结过了,大概的流程就是先设置spinner控件,如下:

Spinner<
android:id="@+id/touchprice"
android:layout_width="190dp"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:dropDownWidth="20dp"
android:prompt="@string/touch" />   <!--这里设置首先显示的是哪个数值 -->


之后可以在代码上,设置默认下拉样式和弹出模式,监听选中的选项,这些都有很多例子可以参考,我的是这样的:

// 第2个下拉列表,有效日期
spinner = (Spinner) view.findViewById(R.id.effectivedate);// 将可选内容与ArrayAdapter连接起来
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, m);</span>
</strong>
// //设置下拉列表的风格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// 将adapter 添加到spinner中
spinner.setAdapter(adapter);

// 添加事件Spinner事件监听
spinner.setOnItemSelectedListener(new SpinnerSelectedListener());

// 设置默认值
spinner.setVisibility(View.VISIBLE);

}

// 使用数组形式操作
class SpinnerSelectedListener implements OnItemSelectedListener {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast tst = Toast.makeText(getView().getContext(),
"您点击了" + m[arg2], Toast.LENGTH_SHORT);
tst.show();

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}


       这样就实现了最基本的系统下拉列表组件,如下:



    是不是感觉特别的丑,特别是如下下面我的edittext是要用圆角来显示的话,就显得对比太大了,所以就到了关键的一步,不要使用系统自带的列表项布局文件,也就是上述的这段代码:

// 将可选内容与ArrayAdapter连接起来
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, m);
// //设置下拉列表的风格
     我们可以替换成我们想要表现的布局格式,下面是我要呈现的样子,就是圆角,然后修改里面默认字体的颜色,改为白色,并且居中显示:

activity_tipsprice_spinner.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:gravity="center"
android:paddingTop="5dp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:textSize="14dp"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"/>


    这个只是设置下拉的文字样式,如果要圆角的话,必须在spinner组件设置android:background设置圆角的样式,如下:

 <Spinner

                android:id="@+id/touchprice"

                android:layout_width="190dp"

                android:layout_height="35dp"

                android:layout_marginLeft="10dp"

                android:background="@drawable/tipspriceshape"

                android:dropDownWidth="20dp"

                android:prompt="@string/touch" />

  贴出圆角的文件代码,tipspriceshape.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<!-- 填充的颜色 -->
<solid android:color="#2B2B2B" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="@dimen/RoundedAmplitude" />

<gradient
android:angle="270"
android:centerColor="#2B2B2B"
android:endColor="#2B2B2B"
android:startColor="#2B2B2B" />
<stroke
android:width="0.01dp"
android:color="#FFFFFF">
</stroke>
</shape>
这样就顺利的完成了需求,改变了默认的样式为圆角,并且文字居中,颜色为白色。截图如下:

(红色的为了保密所需要,欢迎各位前辈指出不足,或者更好的办法)。

=========================分割线,2015年12月18日10:27:04=====================================================

增加一个spinner的功能,当用户点击某一个想要修改的时候,进入到spinner的页面,需要显示上次修改的列表值,示例代码如下:

@Override
public void onDataEvent(final StationEventData data) {//回调接口,不明白的,可以参考我之前的博客
if (data.getEventName()
.equals(IStationEventName.EVENT_NAME_TIME_CHANGE)
&& !isHidden()) {
if (timeView != null) {
Runnable work = new Runnable() {

@Override
public void run() {
if (data.getData().toString().equals("1")) {
timeView.setHint("請重新選擇有效日期");
expireTimeSpinner.setSelection(8);
} else {
timeView.setText(data.getData().toString() + ":00");

SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = format.parse(data.getData().toString());
} catch (ParseException e) {
e.printStackTrace();
}
effectivetime = date;
}
}
};
getHandler().post(work);
}
} else if (data.getEventName().equals(
IStationEventName.EVENT_NAME_TIME_CHANGE_MODIFY)
&& getActivity() != null) {
// 2015年12月18日10:21:36:当用户点击修改某个价格,用来设定下拉列表默认现实的类型
int typeselect = Integer.parseInt(data.getData().toString());
if (typeselect == 0) {
expireTimeSpinner.setSelection(8);
} else if (typeselect == 1) {
expireTimeSpinner.setSelection(6);
} else if (typeselect == 2) {
<span style="color:#ff0000;">expireTimeSpinner.setSelection(7);</span>
} else {
expireTimeSpinner.setSelection(9);
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息