我不明白为什么下面的代码将我的TextView放在屏幕的左上角。根据我对layout_gravity的理解,它似乎应该把我的TextView放在屏幕的底部。
有没有人能帮我解释一下?这种线性布局,尽管看起来非常简单,但却给我带来了许多令人头疼的问题。一件看似如此简单的事情,怎么会变得如此难以掌握呢?
<?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>发布于 2012-04-22 01:06:26
设置为垂直方向的LinearLayout将忽略引用垂直对齐的所有layout_gravity参数。例如,“底部”不起作用,但“中心-水平”会起作用。(始终进行垂直对齐,以便我放置在布局中的其他视图下的布局中的每个视图。)
相对布局可能是可行的,或者您可以使用此代码,它可能非常接近您想要的内容。
<?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进一步解释了这些内容。
发布于 2012-04-22 00:22:10
<?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>您可以使用相对布局,因为您可以轻松地对齐底部
发布于 2012-04-22 00:33:45
我认为这段代码可以做你想做的事情:
<?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>如果你想让文本视图居中,你可以使用:
<?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>希望这能对你有所帮助。
https://stackoverflow.com/questions/10260766
复制相似问题