custom a switch

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值