Android的layout_gravity [英] Android layout_gravity

查看:115
本文介绍了Android的layout_gravity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

任何人都可以请一些线索这对我?这种线性布局,尽管似乎非常简单,害得我无数头痛。如何才能东西看起来很简单,是这样一个皇家疼痛把握?

 <?XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:方向=垂直
        机器人:背景=#FF0000>        <的TextView
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=@字符串/你好
            机器人:layout_gravity =底
            机器人:背景=#FFF
            />    < / LinearLayout中>


解决方案

一个的LinearLayout设置为垂直方向将忽略是指垂直对齐所有layout_gravity参数。因此,例如,底部不会工作,但中心横将起作用。 (垂直取向总是这样做的,在我划归在布局中的其他视图的布局中的每个视图。)

一个相对布局可能是要走的路或者你可以用这个code,它可能是pretty接近你想要什么。

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直
    机器人:背景=#FF0000
>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/你好
        机器人:重力=底
        机器人:layout_weight =1.0
        机器人:背景=#FFF/>< / LinearLayout中>

修改此的博客进一步解释的事情。

I can't understand why the following code places my TextView in the top left corner of my screen. Based on my understanding of layout_gravity, it seems that it should place my TextView at the bottom of my screen instead.

Can anyone please shed some light on this for me? This Linear Layout, despite seeming very simplistic, has caused me numerous headaches. How can something that seems so simple, be such a royal pain to grasp?

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

解决方案

A LinearLayout set to vertical orientation will ignore all layout_gravity parameters that refers to vertical alignment. So for example "bottom" wont work but "center-horizontal" will work. (The vertical alignment is always done so that each view in the layout i placed under the other views in the layout.)

A relative layout might be the way to go or maybe you can use this code that probably is pretty close to what you want.

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

Edit This blog explains things further.

这篇关于Android的layout_gravity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆