【问题标题】:Android Landscape mode cutting off my buttons on topAndroid横向模式切断了我在顶部的按钮
【发布时间】:2011-05-27 07:00:30
【问题描述】:

我的申请有问题。我最近将 ScrollView 添加为 LinearLayout 的父级。当我的设备方向翻转为横向时,我的按钮在顶部被切断。当我向下滚动时,我可以看到最后一个按钮在它的末尾有额外的“黑色空间”,只有顶部被切断。我环顾四周,我不确定为什么会这样。这里有一些图片,您可以了解它的外观。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:gravity="center_vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center">
    <Button 
        android:id="@+id/BeefButton" 
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="center"
        android:background="@drawable/backgroundlayer"
        android:drawableLeft="@drawable/beefbutton"
        android:text="Beef"
        android:textColor="#FFFFFF"
        android:textSize="32px"
        android:textColorHighlight="#FF6600"/> 
    <Button 
        android:id="@+id/PoultryButton" 
        android:layout_height="wrap_content"
        android:layout_gravity="center" 
        android:layout_width="match_parent"
        android:background="@drawable/backgroundlayer"
        android:drawableLeft="@drawable/chickenbutton"
        android:text="Poultry"
        android:textColor="#FFFFFF"
        android:textSize="32px"        
        android:textColorHighlight="#FF6600"/>
    <Button 
        android:id="@+id/FishButton" 
        android:layout_height="wrap_content"
        android:background="@drawable/backgroundlayer"
        android:drawableLeft="@drawable/fishbutton"
        android:text="Fish"
        android:textColor="#FFFFFF"
        android:textSize="32px" 
        android:textColorHighlight="#FF6600"
        android:layout_gravity="center" 
        android:layout_width="match_parent"/>
    <Button 
        android:id="@+id/PorkButton" 
        android:layout_height="wrap_content"
        android:background="@drawable/backgroundlayer"
        android:drawableLeft="@drawable/porkbutton"
        android:text="Pork"
        android:textColor="#FFFFFF"
        android:textSize="32px" 
        android:textColorHighlight="#FF6600"
        android:layout_gravity="center" 
        android:layout_width="match_parent"/>
    <Button 
        android:id="@+id/VegetablesButton" 
        android:layout_height="wrap_content"
        android:background="@drawable/backgroundlayer"
        android:drawableLeft="@drawable/vegetablesbutton"
        android:text="Vegetables"
        android:textColor="#FFFFFF"
        android:textSize="32px" 
        android:textColorHighlight="#FF6600"
        android:layout_gravity="center" 
        android:layout_width="match_parent"/>
</LinearLayout>
</ScrollView>

【问题讨论】:

  • 请发布您的布局。纵向和横向模式是一样的吗?
  • 另外,您不使用 ListView 而不是使用带有 ScrollView 的 LinearLayout 是否有原因?您的列表项是否总是完全相同,并且只有 5 个?
  • 是的,它们完全一样,只有五个项目

标签: android scrollview android-linearlayout


【解决方案1】:

删除您的 linearlayout 上的 android:layout_gravity="center" ,当您将其更改为横向模式时,滚动视图的高度大于您的屏幕尺寸,因此它在 ScrollView 中将线性布局居中,尽管删除它可能不是解决方案,是什么原因造成的,

你可以考虑修改你的布局,使用 listview 来成功支持横向可能更有意义

【讨论】:

  • 除此之外,没有办法让我的项目像这样列在中心,对吧? :-(
  • 你可以在你的 ScrollView 上添加 "layout_gravity="center"" 并让它 wrap_content。当有足够空间时,这将导致 View 在其容器中居中,而在没有空间时正确滚动。
【解决方案2】:

我知道这是一个旧线程,但我花了两天时间解决这个问题。虽然删除“layout_gravity”语句确实有助于切断问题,但关于无法居中的评论也是正确的。最终对我有用的,在我看来是一个 hack,是让滚动视图位于居中的布局之上,两个线性布局作为子级级联。此外,高度和宽度必须是滚动视图和线性布局上的包装内容。这不是最简洁的解决方案,但我尝试了一切都没有成功,包括在对象小于屏幕尺寸宽度时使用 setlayoutparams 在代码中设置重力。我希望这可以帮助别人。

【讨论】:

    【解决方案3】:

    @slim 所说的提示(删除 android:layout_gravity="center")确实有效,但没有居中对齐。

    我的解决方案:

    我通过用 LinearLayout 包裹主长布局并将其 gravity 设置为 center 并让 android:layout_gravity="center" 保留在主布局中来修复它:

    <ScrollView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                >
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center">
    
              <!-- .... the main long layout ... -->
    
            </LinearLayout>
    
    </ScrollView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-06
      • 1970-01-01
      相关资源
      最近更新 更多