【问题标题】:Switch between spinner and listview based on screen orientation根据屏幕方向在微调器和列表视图之间切换
【发布时间】:2014-03-01 22:10:36
【问题描述】:

In my android application I have a spinner containing different calculators, and when a calculator is selected it shows up in a scrollview below.当屏幕设置为水平时,布局会发生变化,滚动视图在右侧,而微调器在左侧,并且都以相同的方式工作。

但是,当屏幕处于水平位置时,列表视图会更好看,但我似乎找不到在 java 代码中进行切换的方法。

有人可以帮忙吗?

这是初始化方法的代码,我想知道是否有一种方法可以插入一个 if 语句,如果屏幕是垂直的,微调器将被初始化,如果屏幕是水平的,则列表视图将被初始化.

private void initialize() {

    valg_vertical = (Spinner) findViewById(R.id.sKalk);

    // Adds the custom look for the spinner
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.custom_spinner, getResources().getStringArray(
                    R.array.calculator));
    adapter.setDropDownViewResource(SpinnerItemLayout);
    valg_vertical.setAdapter(adapter);
    // Adds the custom look for the spinner

    lLayout = (LinearLayout) findViewById(R.id.lInSVKalk);
    valg_vertical.setOnItemSelectedListener(this);
}

【问题讨论】:

    标签: java android listview orientation spinner


    【解决方案1】:

    您有两种可能的解决方案:

    1. portraitlandscape 模式创建不同的布局(它们将分别位于layoutlayout-land 文件夹下)。
    2. 在您的布局中创建一个容器(例如FrameLayout)。并根据方向在运行时为其添加相应的视图。

    无论如何,由于两个方向使用不同的元素,因此您应该在代码中添加以下if 语句:

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
    
    } else {
    
    }
    

    【讨论】:

    • 非常感谢,太完美了!我提出了不同的观点,但现在我看不到如何在运行时在它们之间切换..
    • @jangerhard 好吧,我相信根本原因是NPE?如果是这样,您应该根据方向找到您的视图
    • 我想通了!我没有更改不同组件的 id,现在它可以完美运行了!再次感谢!
    猜你喜欢
    • 2020-04-08
    • 2010-12-28
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多