简体   繁体   English

android absolutelayout对齐中心

[英]android absolutelayout align center

I have a simple android app that contain scrollview and inside it there is absolutelayout and I want them to be in the center. 我有一个简单的android应用程序,其中包含scrollview,并且其中有绝对布局,我希望它们位于中心。 when I see the graphical layout in eclipse the absolutelayout is in the center of the screen like this: 当我在eclipse中看到图形布局时,absolutelayout位于屏幕中央,如下所示:

here is my code: 这是我的代码:

<?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>

Firstly, AbsoluteLayout is deprecated so you probably shouldn't use it. 首先,AbsoluteLayout已过时,因此您可能不应该使用它。 I'd suggest RelativeLayout instead. 我建议改用RelativeLayout。

As for the ScrollView, make sure to supply the android:fillViewport="true" and then set the child layout's layout_width="match_parent" like so: 至于ScrollView,请确保提供android:fillViewport =“ true”,然后像下面这样设置子布局的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>

This will handle the case where the ScrollView's child width is less than the width of the ScrollView itself. 这将处理ScrollView的子宽度小于ScrollView本身的宽度的情况。

You don't have to use Scroll View for this. 您不必为此使用滚动视图。 There are a couple of XML properties you can add to your TextView in order to make it scrollable. 您可以将几个XML属性添加到TextView中,以使其可滚动。

Use this: 用这个:

android:maxLines = "AN_INTEGER"

android:scrollbars = "vertical"

Then in your Java class: 然后在您的Java类中:

yourTextView.setMovementMethod(new ScrollingMovementMethod())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM