android 垂直的开关_Android开发之开关按钮用法示例

本文实例讲述了Android开发之开关按钮用法。分享给大家供大家参考,具体如下:

效果如下:

以下是布局文件:

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/root"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/toggle"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textOff="横向排列"

android:textOn="纵向排列"

android:checked="true"/>

android:id="@+id/switcher"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textOff="横向排列"

android:textOn="纵向排列"

android:thumb="@drawable/thumb"

android:checked="true"/>

android:id="@+id/text"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/button01"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:id="@+id/button02"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:id="@+id/button03"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

活动代码实现:

public class Home extends AppCompatActivity {

ToggleButton toggle ;

Switch switcher ;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);//显示manLayout

toggle = (ToggleButton) findViewById(R.id.toggle);

switcher = (Switch) findViewById(R.id.switcher);

final LinearLayout text = (LinearLayout) findViewById(R.id.text);

CompoundButton.OnCheckedChangeListener onCheckedChangeListener =

new CompoundButton.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(

CompoundButton buttonView, boolean isChecked) {

if (isChecked) {

//设置LinearLayout垂直布局

text.setOrientation(LinearLayout.VERTICAL);

toggle.setChecked(true);

switcher.setChecked(true);

}else {

//设置水平布局

text.setOrientation(LinearLayout.HORIZONTAL);

toggle.setChecked(false);

switcher.setChecked(false);

}

}

};

toggle.setOnCheckedChangeListener(onCheckedChangeListener);

switcher.setOnCheckedChangeListener(onCheckedChangeListener);

}

}

其中switch组建的 thumb:@drawable/thumb项参考自://www.jb51.net/article/159100.htm

希望本文所述对大家Android程序设计有所帮助。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值