滚动视图仅在Android中的纵向模式下禁用

时间:2013-02-06 07:44:01

标签: android

我的xml中有一个滚动视图,但滚动视图只能在手机的横向模式下工作,而不能在手机的纵向模式下工作。这是可能的,如果可能的话,我应该使用xml文件或通过编程方式。如果代码需要请问我。谢谢

这是xml文件(纵向模式):

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollview1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#1e90ff"
    tools:context=".HomeActivity"
    android:scrollbars="none"
    >

<LinearLayout
    android:id="@+id/layout01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#a9a9a9"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="1dp"
        android:background="@drawable/my_tauky_button_img" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="1dp"
        android:background="@drawable/explore_button_img"
        android:text="" />

    <Button
        android:id="@+id/button3"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="1dp"
        android:background="@drawable/create_tauky_button_img" />

    <Button
        android:id="@+id/button4"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="1dp"
        android:background="@drawable/my_blauky_button_img" />

    <Button
        android:id="@+id/button5"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="@drawable/profile_button_img" />
</LinearLayout>

</ScrollView>

4 个答案:

答案 0 :(得分:1)

最简单的解决方案是检查:

getResources().getConfiguration().orientation;
如果方向为onCreate(),则在ScrollView

并停用portrait。请参阅此处的参考:http://developer.android.com/reference/android/content/res/Configuration.html#orientation

它的工作原理是因为默认情况下Activities会在配置更改时重新启动(包括更改方向),因此您不必监听此事件。

只需修改您的onCreate()

即可
@Override
protected void onCreate(Bundle savedState) {
    super.onCreate(savedState);
    setContentView(R.layout.activity_layout);

    // disable ScrollView in portrait mode
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        ScrollView scrollView = (ScrollView)findViewById(R.id.scrollview1);
        scrollView.setEnabled(false);
    }
}

其中activity_layout是您的布局文件名(没有.xml扩展名)。就是这样!

答案 1 :(得分:0)

创建两个不同的xml layouts.one用于纵向模式(将其放在默认的“布局”文件夹中)和一个用于横向(创建“layout-land”文件夹并将其放在那里),并仅添加滚动视图第二个。

答案 2 :(得分:0)

试试这个,

  scrollView.setVerticalScrollBarEnabled(false);

答案 3 :(得分:0)

试试这段代码:)

switch (getResources().getConfiguration().orientation) {
        case Configuration.ORIENTATION_PORTRAIT:
            ScrollView scrollView = (ScrollView)findViewById(R.id.scrollview1);
            scrollView.setEnabled(false);
            break;

}