【问题标题】:Have some buttons show only in landscape mode让某些按钮仅在横向模式下显示
【发布时间】:2019-04-09 12:27:59
【问题描述】:

我想让特定按钮仅在横向模式下显示 - 不显示或纵向可用。我有横向和纵向的单独 xml 文件

我尝试使用 OrientationEventListener,在它运行时,我检查了设备方向是否为横向 - 如果是,我在其上调用了 findViewById,但它由于 NullPointer 而崩溃。到目前为止我的代码:

Button landscapeTest;

public boolean isInLandscape() {
int orientation = getResources().getConfiguration().orientation;
return orientation == Configuration.ORIENTATION_LANDSCAPE;

OrientationEventListener orientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI) {
@Override
public void onOrientationChanged(int orientation) {
   boolean isInLandscape = isInLandscape();
   if (isInLandscape) {
       landscapeTest = findViewById(R.id.button_landscape);
       landscapeTest.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Log.v("landscapeButton", "I am working!!!");
           }
       });
   }
}
};

预期 - 当我更改设备方向(从纵向到横向)时,我应该在 UI 中看到 ID 为 button_landscape 的按钮,当我点击它时,我应该看到“我正在工作!!!”在日志中

实际:当我更改设备方向(从纵向到横向)时,由于找不到按钮,它会与 NullPointer 一起崩溃。

【问题讨论】:

  • landscapeTest = findViewById(R.id.button_landscape); 放入onCreate。当方向改变时,通过在landscapeTest.setVisibility(View.GONE / View.VISIBLE) 之间进行选择来设置可见性
  • 最好的做法是,如果你想在不同的模式下显示不同的视图,你可以在 layout-land 文件夹下创建单独的布局文件。
  • 您还想将OnClickListener 放在onCreate 中,只处理屏幕旋转时的可见性,而不是OnClickListener
  • 在纵向布局中放置相同的按钮,但将它们的可见性设置为GONE

标签: android button screen-orientation


【解决方案1】:

崩溃的原因是当您更改方向时,Android 会重新启动 Activity。并再次致电OnCreate()

请阅读Handle Runtime Changes

1) 如果你想在不同的模式下显示不同的布局,我建议为纵向模式(layout/layout.xml)和横向模式(layout-land/layout.xml)创建单独的文件,名称相同。所以android会处理方向变化。

2) 如果您不想创建两个单独的布局文件并从类文件中处理它。请将您的代码移动到OnCreate() 并检查布局是来自OnCreate() 的纵向还是横向。因为onClickListener 不属于onOrientationChanged。它还将解决NullPointerException 的问题。根据方向,您可以隐藏/显示按钮。

【讨论】:

  • 您的第一个解决方案仍将返回NullPointerException,因为Button 将在layout.xml(纵向)而不是其他(横向)中可用。拨打landscapeTest = findViewById(R.id.button_landscape); 时,他仍会收到NullPointerException。在这种情况下,最好使用可见性来处理它。
  • 您需要检查按钮是否为空。那么只有你可以使用 setOnClickListener() 。通过使用不同的文件,Android 会处理 Orientation Changes,但 null 检查和实现需要您手动处理。
【解决方案2】:

就像我在评论部分提到的那样。您只想在方向更改时处理 Button 的可见性。

landscapeTest = findViewById(R.id.button_landscape);

应该在OnCreate 以及您的OnClickListener 中。

这是一个例子:

public class SomeActivity extends Activity {    

Button landscapeTest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_some);

    landscapeTest = findViewById(R.id.button_landscape);
    landscapeTest.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           Log.v("landscapeButton", "I am working!!!");
       }
    });

    OrientationEventListener orientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI) {
    @Override
    public void onOrientationChanged(int orientation) {
        boolean isInLandscape = isInLandscape();
        if (isInLandscape) {
        landscapeTest.setVisibility(View.GONE);
        }else{
        landscapeTest.setVisibility(View.VISIBLE);
        }
    }

}

【讨论】:

    【解决方案3】:

    试试这个会帮助你:

    @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                landscapeTest.setVisibility(View.VISIBLE);
            } else {
                landscapeTest.setVisibility(View.GONE);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-17
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 2014-03-26
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多