android编程—界面横竖向切换

 时间:2026-02-17 04:30:26

1、首先打开Android编程工具,其实无所谓哪一种工具,我用的Eclipse

2、先添加几个控件,设置为垂直显示,代码如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"       android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"        android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    tools:context=".MainActivity" >    <ToggleButton         android:id="@+id/toggle"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textOff="水平分布"        android:textOn="垂直分布"        android:checked="true"        />    <LinearLayout         android:id="@+id/test"        android:orientation="vertical"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        >        <Button              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="第一个按钮"/>        <Button              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="第二个按钮"/>        <Button              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="第三个按钮"/>    </LinearLayout></LinearLayout>

显示效果,如图显示

android编程—界面横竖向切换

3、然后编写核心代码,代码如下

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);         ToggleButton toggle = (ToggleButton) findViewById(R.id.toggle);            final LinearLayout test = (LinearLayout)findViewById(R.id.test);            toggle.setOnCheckedChangeListener(new OnCheckedChangeListener(){                @Override                public void onCheckedChanged(CompoundButton buttonView,                        boolean isChecked) {                    if(isChecked){                        //垂直                        test.setOrientation(1);                    }else{                        //水平                        test.setOrientation(0);                    }                }                            });        }

4、然后启动android虚拟机,进行调试,效果如下图所示

android编程—界面横竖向切换

  • 被困电梯时该怎样自救
  • 怎样进行股票期货联动操作
  • 目鱼干炖腐竹排骨汤的做法
  • ㊙️入口即化的草莓白玉卷‼️敲好吃
  • 高速原路返回如何收费
  • 热门搜索
    新学期新气象的手抄报 科普手抄报内容 端午节英语手抄报 重阳节手抄报内容大全 缅怀先烈的手抄报 绿色环保手抄报图片 关于网络安全的手抄报 铁人精神手抄报 红红火火过大年手抄报 交通手抄报内容