2010-12-17 94 views
12

我的应用程序出现问题。我最近添加了ScrollView作为LinearLayout的父项。当将我的设备方向翻转到横向时,我的按钮在顶部被切断。当我向下滚动时,我可以看到最后一个按钮处于最后的“黑色空间”,只有顶部被切断。我环顾四周,我不确定为什么会发生这种情况。这里有一些图片,所以你可以了解它的外观。Android风景模式切断我顶部的按钮

<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> 
+0

请发表您的布局。你对肖像和风景模式有相同的吗? – 2010-12-17 18:24:20

+0

另外,是否有一个原因,你没有使用ListView,而不是一个带有ScrollView的LinearLayout?你的列表项目总是完全一样,只有5个? – 2010-12-17 18:35:04

+0

是的他们是完全一样的方式,只有五个项目 – SickNick 2010-12-17 18:41:56

回答

19

您的LinearLayout删除android:layout_gravity="center",当你将其更改为横向模式滚动视图的高度比屏幕尺寸更大,所以其中心在滚动型的LinearLayout中,虽然删除它可能不是解决办法,这是什么原因造成这一点,

您可以考虑修改您的布局,它可能会更有意义使用列表视图,以便能够成功地支持横向

+1

没有办法让我的项目在中心那样列出,除此之外,虽然正确吗? :-( – SickNick 2010-12-17 19:52:56

+0

你可以在你的ScrollView上添加“layout_gravity =”center“”,并将其设置为wrap_content。当没有足够的空间并且正确滚动时,这将导致视图位于容器的中心。 – Graeme 2014-09-26 15:07:59

2

我知道这是一个古老的线程,但我花两天就解决这个问题。虽然删除“layout_gravity”语句确实有助于解决问题,但无法集中的评论也是如此。最终,对我而言,我认为这是一种黑客行为,它将scrollview放置在布局顶部,并将两个linearlayouts作为儿童级联。此外,高度和宽度必须在scrollview和linearlayouts上包装内容。这不是最好的解决方案,但我尝试了一切都没有成功,包括如果对象小于屏幕尺寸宽度,则使用setlayoutparams设置代码中的重力。我希望这可以帮助别人。

0

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

我的解决方案:

我固定它通过包裹主力多头布局与它的gravityLinearLayoutcenter,让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>