首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓layout_gravity

安卓layout_gravity
EN

Stack Overflow用户
提问于 2012-04-22 00:18:05
回答 4查看 2.9K关注 0票数 1

我不明白为什么下面的代码将我的TextView放在屏幕的左上角。根据我对layout_gravity的理解,它似乎应该把我的TextView放在屏幕的底部。

有没有人能帮我解释一下?这种线性布局,尽管看起来非常简单,但却给我带来了许多令人头疼的问题。一件看似如此简单的事情,怎么会变得如此难以掌握呢?

代码语言:javascript
复制
 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="#FF0000">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello"
            android:layout_gravity="bottom"
            android:background="#FFF"
            />    

    </LinearLayout>
EN

回答 4

Stack Overflow用户

发布于 2012-04-22 01:06:26

设置为垂直方向的LinearLayout将忽略引用垂直对齐的所有layout_gravity参数。例如,“底部”不起作用,但“中心-水平”会起作用。(始终进行垂直对齐,以便我放置在布局中的其他视图下的布局中的每个视图。)

相对布局可能是可行的,或者您可以使用此代码,它可能非常接近您想要的内容。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#FF0000"
>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:gravity="bottom"
        android:layout_weight="1.0"
        android:background="#FFF" />

</LinearLayout>

编辑blog进一步解释了这些内容。

票数 2
EN

Stack Overflow用户

发布于 2012-04-22 00:22:10

代码语言:javascript
复制
 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FF0000">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello"
            android:layout_alignParentBottom="true"
            android:background="#FFF"
            />    

    </RelativeLayout>

您可以使用相对布局,因为您可以轻松地对齐底部

票数 0
EN

Stack Overflow用户

发布于 2012-04-22 00:33:45

我认为这段代码可以做你想做的事情:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="bottom"
    android:background="#FF0000">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:background="#FFF"
        />    

</LinearLayout>

如果你想让文本视图居中,你可以使用:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="bottom"
    android:background="#FF0000">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:layout_gravity="center"
        android:background="#FFF"
        />    

</LinearLayout>

希望这能对你有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10260766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档