我正在尝试在我的应用程序中创建一个弹出窗口;但是,当按下按钮时,不会出现弹出窗口。没有错误,应用程序也没有崩溃。所使用的代码对我之前用于弹出窗口的一些代码进行了稍微修改,该代码可以工作。
ISOButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater2 = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater2.inflate(R.layout.isochanger, null);
final PopupWindow popupwindow = new PopupWindow(popupView, width, height, focusable);
popupwindow.showAtLocation(view, Gravity.CENTER, 0, 0);
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupwindow.dismiss();
return true;
}
});
}
});
完整的 Activity 是:
package com.example.startrailscamera;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.camera2.*;
import android.os.Bundle;
import android.view.*;
import android.util.*;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
TextureView textureView;
ImageButton imageButton, singleImageButton;
TextView ISOText;
SeekBar seekBar;
Integer maxISO, minISO, progress, ISO, width, height, ISO2, ISO3, ISO4, ISORange, ISO5;
Range<Integer> rangeISO;
String cameraId;
Button ISOButton, isomin, iso2, iso3, iso4, iso5, isomax;
boolean focusable;
View popupView;
LayoutInflater inflater, inflater2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.cameralayout, null);
width = LinearLayout.LayoutParams.WRAP_CONTENT;
height = LinearLayout.LayoutParams.WRAP_CONTENT;
focusable = true;
final PopupWindow popupwindow = new PopupWindow(popupView, width, height, focusable);
popupwindow.showAtLocation(view, Gravity.CENTER, 0, 0);
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupwindow.dismiss();
return true;
}
});
}
});
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
return;
}
try {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
maxISO = rangeISO.getUpper();
minISO = rangeISO.getLower();
rangeISO = characteristics.get(CameraCharacteristics.SENSOR_INFO_SENSITIVITY_RANGE);
Range<Long> exposureTimeRange = characteristics.get(CameraCharacteristics.SENSOR_INFO_EXPOSURE_TIME_RANGE);
} catch (CameraAccessException e) {
e.printStackTrace();
}
ISORange = maxISO - minISO;
ISO2 = minISO + (ISORange/5);
ISO3 = minISO + (ISORange/5*2);
ISO4 = minISO + (ISORange/5*3);
ISO5 = minISO + (ISORange/5*4);
isomin.setText(minISO);
iso2.setText(ISO2);
iso3.setText(ISO3);
iso4.setText(ISO4);
iso5.setText(ISO5);
isomax.setText(maxISO);
ISOButton = findViewById(R.id.ISOButton);
ISOButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater2 = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater2.inflate(R.layout.isochanger, null);
final PopupWindow popupwindow = new PopupWindow(popupView, width, height, focusable);
popupwindow.showAtLocation(view, Gravity.CENTER, 0, 0);
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupwindow.dismiss();
return true;
}
});
}
});
}
}
我试图显示的 XML 文件是
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:id="@+id/iso3"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_toStartOf="@+id/iso4"
android:text="3" />
<Button
android:id="@+id/isomin"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="35dp"
android:layout_marginEnd="0dp"
android:layout_toStartOf="@+id/iso2"
android:text="1" />
<Button
android:id="@+id/iso4"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_toStartOf="@+id/iso5"
android:text="4" />
<Button
android:id="@+id/iso5"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_toStartOf="@+id/isomax"
android:text="5" />
<Button
android:id="@+id/isomax"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="45dp"
android:text="6" />
<Button
android:id="@+id/iso2"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_toStartOf="@+id/iso3"
android:text="2" />
</RelativeLayout> ```
最佳答案
问题在于宽度
和高度
。
我针对这个特定问题评论了不必要的代码。 这是您的工作 Activity 代码
package com.andruid.magic.imagesegmentationlib;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.camera2.*;
import android.os.Bundle;
import android.view.*;
import android.util.*;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
TextureView textureView;
ImageButton imageButton, singleImageButton;
TextView ISOText;
SeekBar seekBar;
Integer maxISO, minISO, progress, ISO, width, height, ISO2, ISO3, ISO4, ISORange, ISO5;
Range<Integer> rangeISO;
String cameraId="0";
Button ISOButton, isomin, iso2, iso3, iso4, iso5, isomax;
boolean focusable = true;
View popupView;
LayoutInflater inflater, inflater2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.activity_main, null);
width = LinearLayout.LayoutParams.WRAP_CONTENT;
height = LinearLayout.LayoutParams.WRAP_CONTENT;
focusable = true;
final PopupWindow popupwindow = new PopupWindow(popupView, width, height, focusable);
popupwindow.showAtLocation(view, Gravity.CENTER, 0, 0);
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupwindow.dismiss();
return true;
}
});
}
});
/* CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
return;
}
try {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
maxISO = rangeISO.getUpper();
minISO = rangeISO.getLower();
rangeISO = characteristics.get(CameraCharacteristics.SENSOR_INFO_SENSITIVITY_RANGE);
Range<Long> exposureTimeRange = characteristics.get(CameraCharacteristics.SENSOR_INFO_EXPOSURE_TIME_RANGE);
} catch (CameraAccessException e) {
e.printStackTrace();
}
ISORange = maxISO - minISO;
ISO2 = minISO + (ISORange/5);
ISO3 = minISO + (ISORange/5*2);
ISO4 = minISO + (ISORange/5*3);
ISO5 = minISO + (ISORange/5*4);
isomin.setText(minISO);
iso2.setText(ISO2);
iso3.setText(ISO3);
iso4.setText(ISO4);
iso5.setText(ISO5);
isomax.setText(maxISO);
*/
ISOButton = findViewById(R.id.ISOButton);
ISOButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater2 = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater2.inflate(R.layout.isochanger, null);
width = LinearLayout.LayoutParams.WRAP_CONTENT;
height = LinearLayout.LayoutParams.WRAP_CONTENT;
Log.d("TAG",""+width);
final PopupWindow popupwindow = new PopupWindow(popupView, width, height, focusable);
popupwindow.showAtLocation(view, Gravity.CENTER, 0, 0);
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupwindow.dismiss();
return true;
}
});
}
});
}
}
这是输出屏幕截图。
希望它可以帮助您解决。
关于java - 创建弹出窗口时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58521034/