2014-09-22 69 views
1

我有一个简单的android应用程序,它包含scrollview,里面有absolutelayout,我希望它们位于中心。当我在Eclipse中看到图形化的布局absolutelayout是在屏幕的像这样的中心:android absolutelayout align center

这里是我的代码:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="400dp" 
android:layout_height="fill_parent" 
android:background="@drawable/bg" 
android:gravity="center_vertical" > 

<AbsoluteLayout 
    android:id="@+id/AbsoluteLayout1" 
    android:layout_width="284dp" 
    android:layout_height="562dp" 
    android:layout_marginBottom="40dp" 
    android:layout_marginLeft="18dp" 
    android:layout_marginRight="18dp" 
    android:layout_marginTop="26dp" 
    android:background="@drawable/bg4" 
    > 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="244dp" 
     android:layout_height="500dp" 
     android:layout_x="20dp" 
     android:layout_y="81dp" 
     android:text="Info here" 
     android:textColor="#ffffff" /> 

    </AbsoluteLayout> 

</ScrollView> 

回答

0

首先,AbsoluteLayout已经过时,所以你可能不应该使用它。我会建议RelativeLayout。

对于滚动型,确保提供了android:fillViewport =“真”,然后设置子布局的layout_width =“match_parent”像这样:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    android:background="#444444"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical" > 

     <Button android:id="@+id/button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:text="Button"/> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_below="@id/button" 
      android:text="Info here" 
      android:textColor="#ffffff" /> 

    </RelativeLayout> 

</ScrollView> 

这将处理的情况下滚动型的子宽度小于ScrollView本身的宽度。

+0

我有一个滚动型里面,因为我不能只用滚动型和里面的TextView或者如果我没有按钮这会给我错误,这就是为什么我使用滚动视图内的布局 – 2014-09-22 22:31:49

+0

我明白,ScrollView只能有一个孩子,但我想知道为什么你需要ScrollView? – 2014-09-22 22:44:33

+0

因为我使用的是长段落,我尝试使用RelativeLayout而不是absolutelayout,但它不起作用,它仍然向左。 – 2014-09-22 23:39:58

0

您不必为此使用滚动视图。有几个XML属性可以添加到TextView中以使其可滚动。

使用此:

android:maxLines = "AN_INTEGER" 

android:scrollbars = "vertical" 
在Java类

然后:

yourTextView.setMovementMethod(new ScrollingMovementMethod())