Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.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
Java 如何在android上使用代码定位特定布局?_Java_Android - Fatal编程技术网

Java 如何在android上使用代码定位特定布局?

Java 如何在android上使用代码定位特定布局?,java,android,Java,Android,我正在开发一个应用程序,其中线性布局中的Imagebutton源在portait和横向模式下是不相似的。这使我在xml中设置了两个可绘制的资源文件,一个引用横向,另一个引用纵向。 然而,在我的代码中,我也需要引用这些可绘制的资源文件,以便我的小部件响应单击。我是否可以实现一种逻辑来分别引用portait和横向中的imagebuttons 纵向模式的我的布局(sw360端口) 如你所见,我需要为我的景观布局设置图像资源,例如 eventWindowButton.setImageResource(R

我正在开发一个应用程序,其中线性布局中的Imagebutton源在portait和横向模式下是不相似的。这使我在xml中设置了两个可绘制的资源文件,一个引用横向,另一个引用纵向。 然而,在我的代码中,我也需要引用这些可绘制的资源文件,以便我的小部件响应单击。我是否可以实现一种逻辑来分别引用portait和横向中的imagebuttons

纵向模式的我的布局(sw360端口)

如你所见,我需要为我的景观布局设置图像资源,例如

eventWindowButton.setImageResource(R.drawable.event_active2);  videoWindowButton.setImageResource(R.drawable.video_active2);

请帮助我

就像您为
sw360端口
sw360陆地
创建了特定布局一样,您可以为这些版本创建可绘制文件,并将其放置在
drawable-sw360dp-port
drawable-sw360dp-land文件夹中

您可以使用OnConfiguration Changed来了解当前版本根据设备的方向,您可以做您想做的事情

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
请参阅这些检查,并特别添加代码
  public void showEvents(View view) {
       if(!eventButtonClicked ) {
           eventWindowButton.setImageResource(R.drawable.event_active);
           audioWindowButton.setImageResource(R.drawable.audio_style);
           moreWindowButton.setImageResource(R.drawable.more_style);
           listView.setVisibility(View.VISIBLE);
           imageViewAudio.setVisibility(View.INVISIBLE);
           playAudioButton.setVisibility(View.INVISIBLE);
           eventButtonClicked = true;
           audioButtonClicked = false;
       } else {
           eventButtonClicked = false;
       }
    }

    public void clickAudioButton(View view) {
        if (boundToAudioService) {
            boundToAudioService = false;
            playAudioButton.setImageResource(R.drawable.stopbutton_style);
          audioService.play();
        } else {
            playAudioButton.setImageResource(R.drawable.playbutton_style);
            audioService.pause();
            boundToAudioService = true;

        }
    }
 public void showAudio(View view) {
        if (!audioButtonClicked) {
            audioButtonClicked = true;
            eventButtonClicked = false;
            audioWindowButton.setImageResource(R.drawable.audio_active);
            eventWindowButton.setImageResource(R.drawable.event_style);
            moreWindowButton.setImageResource(R.drawable.more_style);
            listView.setVisibility(View.INVISIBLE);
            imageViewAudio.setVisibility(View.VISIBLE);
            playAudioButton.setVisibility(View.VISIBLE);

        } else {
            audioButtonClicked = false;
        }

        }
eventWindowButton.setImageResource(R.drawable.event_active2);  videoWindowButton.setImageResource(R.drawable.video_active2);
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}