Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在一个java文件中链接两个XML布局_Android_Xml - Fatal编程技术网

Android 在一个java文件中链接两个XML布局

Android 在一个java文件中链接两个XML布局,android,xml,Android,Xml,我正在创建一个手电筒应用程序…我想在一个java文件中链接两个布局…一个布局是当手电筒未点亮时…另一个布局显示照明手电筒…我已经对其进行了操作,现在当我按下按钮时,它将切换到另一个布局…(照明的一个)…但当我单击按钮时,不会返回到第一个…怎么办? 这是密码 我的XML1: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.an

我正在创建一个手电筒应用程序…我想在一个java文件中链接两个布局…一个布局是当手电筒未点亮时…另一个布局显示照明手电筒…我已经对其进行了操作,现在当我按下按钮时,它将切换到另一个布局…(照明的一个)…但当我单击按钮时,不会返回到第一个…怎么办? 这是密码

我的XML1:

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/fl"
tools:context=".Flash" >

<Button
    android:id="@+id/ib2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/bu" />
@覆盖
公共void onClick(视图v){
//TODO自动生成的方法存根
if(cam==null){
setContentView(R.layout.main2);
cam=摄影机。打开();
para=cam.getParameters();
段落设置闪光模式(参数闪光模式);
凸轮参数设置(第2段);
}否则{
setContentView(R.layout.main);//您忘记了这一行
段落设置闪光模式(参数:闪光模式关闭);
凸轮参数设置(第2段);
凸轮释放();
cam=null;
}
}
退房


这是一个例子。

为什么不在单击按钮时更改背景图像呢

  • 为按钮创建onClickListener
  • 创建一个布尔变量,点击一个按钮就会触发手电筒的打开和关闭
  • 检查手电筒是否已“触发”后更改背景

  • 希望我正确理解了您的问题,您会发现这很有帮助。

    在xml中,第一个按钮是ib2,第二个按钮是ib4,它没有单击侦听器,因此我相信它永远不会调用您的单击侦听器,您还必须向ib4按钮添加一个单击侦听器,以便接收来自该按钮的请求。您已经给了我一个要遵循的路径……但我不知道当我按下按钮时如何介绍切换器……任何帮助都会很明显……您的两个布局现在应该位于ViewSwitcher内的一个布局文件中。两个布局都有一个按钮-为每个按钮指定一个唯一的ID。您的活动中应该有一个全局变量,引用布局中的ViewSwitcher。为两个按钮分配一个OnClickListener。当第一个布局中的按钮按下call switcher.showNext()时,当第二个布局中的按钮按下call switcher.showPrevious().Done!!。。。正在工作…非常感谢…:D…但当我按下“后退”按钮时,应用程序正在崩溃..:/Done with the error tooo…我也必须在onpause中使用if-else语句…工作正常…已解决!!。。。谢谢,伙计们
        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/fl1"
    android:orientation="vertical" >
    
    <Button
        android:id="@id/ib2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/bu" />