代码根据老罗android开发视频教程中的思路,自己敲了一下,又搞定一个控件的用法。
学到的知识:
1、ToggleButton的用法
2、LinearLayout的嵌套布局方式。
UI_ToggleButtonActivity.java源码文件:
package com.jinhoward.UI_ToggleButton;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ToggleButton;
public class UI_ToggleButtonActivity extends Activity {
private ToggleButton myToggleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ui__toggle_button);
myToggleButton=(ToggleButton)this.findViewById(R.id.togglebutton);
final LinearLayout linearLayout = (LinearLayout)this.findViewById(R.id.buttonlayout);
//设置监听器
myToggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
//1表示VERTICAL纵向布局
linearLayout.setOrientation(1);
}else {
//0表示VERTICAL横向布局
linearLayout.setOrientation(0);
}
}
});
}
}
activity_ui__toggle_button.xml文件源代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ToggleButton
android:id="@+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textOff="横向排列按钮"
android:textOn="纵向排列按钮(默认)" >
</ToggleButton>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/buttonlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3" />
</LinearLayout>
</LinearLayout>
效果图2张如下:
源码下载:http://download.csdn.net/detail/jinhoward/5672075