‘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?