1

我在我的 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

4 回答 4

1

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

getResources().getConfiguration().orientation;

如果方向是. inonCreate()并禁用. 请参阅此处的参考:http: //developer.android.com/reference/android/content/res/Configuration.html#orientationScrollViewportrait

它之所以有效,是因为默认情况下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名)。而已!

于 2013-02-06T07:48:27.137 回答
0

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

于 2013-02-06T07:48:41.740 回答
0

尝试这个,

  scrollView.setVerticalScrollBarEnabled(false);
于 2013-02-06T07:49:11.310 回答
0

试试这个代码:)

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

}
于 2013-02-06T07:50:04.787 回答