custom a switch
来源:互联网 发布:pc群控手机源码 编辑:程序博客网 时间:2024/05/17 08:34
‘cuz the switch android provides is in solid height. it’s too narrow to contain my texton/off.
I work out a solution building a custom switch.
there are three steps:
1/ prepare a xml (a relative layout and a textview which will slide)
2/ write a class to control the xml layout.
3/ include this class to other layout where you want to place.
details:
1/ customswitch.xml –which is include in singletest.xml layout.
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="150dp"android:layout_height="60dp"android:background="@drawable/button_pressed"> <TextView android:id="@+id/slidetext" android:layout_width="75dp" android:layout_height="60dp" android:background="#009cc2" android:textColor="#ffffff" android:textSize="25sp" android:gravity="center" /></RelativeLayout>
2/ SingleTest.java (class
public class SingleTest extends LinearLayout { private TextView tv; private boolean switch_on; private RelativeLayout.LayoutParams params_left,params_right; private String texton,textoff; private DatePicker datePicker; private TimePicker timePicker; public SingleTest(Context context, AttributeSet attr) { super(context,attr);// LayoutInflater.from(context).inflate(R.layout.singletest, this); View view = inflate(context,R.layout.singletest,this); tv = (TextView) findViewById(R.id.slidetext); datePicker = (DatePicker) findViewById(R.id.date_picker); timePicker = (TimePicker) findViewById(R.id.time_picker); params_left = new RelativeLayout.LayoutParams(57,60); params_right = new RelativeLayout.LayoutParams(57,60); params_left.addRule(RelativeLayout.ALIGN_PARENT_LEFT); params_right.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); switch_on = false; texton = getResources().getString(R.string.timing_test); textoff = getResources().getString(R.string.immediate_test); tv.setText(textoff); tv.setLayoutParams(params_left); datePicker.setEnabled(false); timePicker.setEnabled(false); timePicker.setIs24HourView(true); tv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(!switch_on) { tv.setText(texton); tv.setLayoutParams(params_right); switch_on = true; Log.d("xjj","on"); datePicker.setEnabled(true); timePicker.setEnabled(true); } else { tv.setText(textoff); tv.setLayoutParams(params_left); switch_on = false; Log.d("xjj","off"); datePicker.setEnabled(false); timePicker.setEnabled(false); } } }); }}
3/ the_layout_you_want.xml (include class like
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffee" android:orientation="vertical"> <include android:id="@+id/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" layout="@layout/grean"/> <com.example.grean.myapplication.SingleTest android:layout_width="match_parent" android:layout_height="90dp"/> <com.example.grean.myapplication.LoopTest android:layout_width="match_parent" android:layout_height="90dp"/></LinearLayout>
okay, now it looks pretty.
ques: how to control a element in other layout?
0 0
- custom a switch
- Implementing a Custom Component
- Creating a Custom View
- Custom a SimpleAsyncTask
- Implementing a Custom Request
- Creating a custom data descriptor
- Create a Custom Master Page
- Building a custom Flex preloader
- Tutorial: Creating a custom button
- Defining a Custom Input Source
- Hack3--Creating a custom ViewGroup
- Create a custom "Big Notifications"
- Refactoring a Switch Statement
- switch-case a new way
- Using Strings with a Switch
- Creating programmatically a switch (UISwitch)
- Creating programmatically a switch (UISwitch)
- Setting up a Custom Membership Provider
- duilib加载资源
- 帧率、码流、分辨率
- UVA 156Ananagrams
- HDU 5692 Snacks DFS序 + 线段树区间更新
- easyui datagrid 行编辑功能
- custom a switch
- java基础---定时任务timer
- QString包含中文时与char *转换
- 重庆小面
- request.getRemoteAddr()怎么获取访问者的ip啊?
- 状态模式
- LTP(Linux Test Project)介绍
- 两种线程池:THREAD_POOL_EXECUTOR和SERIAL_EXECUTOR
- 拔打电话操作