Android中将控件放到线性布局的任意位置(三)

orientationlayout_gravity之间鱼和熊掌的关系?

  • 从前面介绍中我们知道,在之前介绍中,我们想用属性layout_gravity把蓝色TextView这个控件放到屏幕右下角,但是失败了。不能向右放的原因,我们在Android中将控件放到线性布局的任意位置(二)分析了,是因为,该TextView已经在宽度方面充满了屏幕,不再具有向右的空间了。
  • 然而向下是有足够空间的,那么是哪儿出了问题,导致我们不能实现预期目标呢,将布局代码稍微做下修改,将TextView的宽度修改为与其“内容”一样宽(这样,TextView便有了向右靠的空间,注意对比其与之前的区别),并用layout_gravity使之靠右(为便于观察,我将字体设置得比较大):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_one"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    android:orientation="vertical"
    tools:context=".MainActivity"
   >

    <TextView
        android:textSize="25sp"
        android:id="@+id/tv_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#0000ff"
        android:text="第一个TextView"
        android:textColor="#ffffff"
        android:layout_gravity="right"
        />
</LinearLayout>
  • 上述代码表现效果为:


    1.png
  • 可以发现,属性layout_gravity确实能够让TextView按预想的“放置”,那么竖直方向不能让控件靠着底部,莫不是有什么“冲突了”?

  • 注意属性android:orientation,它的意思嘛,是方向,在布局中声明该属性,则在该布局容器之中的子控件都会按照该属性的值布局,这里需要了解一下:

  1. 在所有布局中,默认情况下,它的子控件会从父控件的左上角开始布局(这个方向,和计算机领域,屏幕的方向是相同的,从左上角开始,向右为X正方向,向下为Y轴正方向,左上角坐标为(0,0),相应的,右下角为(maxWidth,maxHeight)(两者分别代表屏幕最大宽度、高度))
  2. 线性布局(LinearLayout),顾名思义,它的子控件会按照线性方向(X或Y方向)布局,至于是向X(水平)方向还是(竖直)方向布局,则就是有android:orientation指定的,默认情况下,线性布局布局会按照水平方向布局。
  • 为说明,取消该属性,并增加一个TextViw:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
tools:context=".MainActivity"
    >
<TextView
    android:textSize="25sp"
    android:id="@+id/tv_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#0000ff"
    android:text="第一个TextView"
    android:textColor="#ffffff"
    android:layout_gravity="right"
    />
    <TextView
        android:textSize="25sp"
        android:id="@+id/tv_tow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#555555"
        android:text="第二个TextView"
        android:textColor="#ffffff"
        android:layout_gravity="right"
        />
</LinearLayout>
  • 上述代码表现为:
    2.png
  • 注意观察,留意到: 我们并没有删除属性android:layout_gravity="right"的,按照修改之前的逻辑,第一个、第二个TextView都应该靠右才对,然而事实是他们都从左边,开始排过来,这似乎就是问题所在

  • 我们之前说过,默认情况下,线性布局子控件是水平方向布局的,也就是相当于在LinearLayout添加了android:orientation="horizontal",那么也就是说:
    1. 当父布局的属性为android:orientation="horizontal"时,子控件属性android:layout_gravity="right"将失效

  • 注意到之前我们LinearLayout的布局一直都是: android:orientation="vertical",那个时候,该线性布局子控件TextView的属性:android:layout_gravity="bottom"是失效的,这样我们有:
    2. 当父布局的属性为android:orientation="vertical"时,子控件属性android:layout_gravity="bottom"将失效

  • 上述猜想是我们实际操作得来,那么在组合:

  1. 父布局:android:orientation="horizontal",子控件:android:layout_gravity="bottom"
  2. 父布局:android:orientation="vertical",子控件:android:layout_gravity="right"
    会生效吗,我们试试:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_one"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    tools:context=".MainActivity"
    android:orientation="horizontal"
    >

    <TextView
        android:id="@+id/tv_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="#0000ff"
        android:text="第一个TextView"
        android:textColor="#ffffff"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tv_tow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="#555555"
        android:text="第二个TextView"
        android:textColor="#ffffff"
        android:textSize="25sp" />
</LinearLayout>

效果:
3.png

代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_one"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/tv_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="#0000ff"
        android:text="第一个TextView"
        android:textColor="#ffffff"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tv_tow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="#555555"
        android:text="第二个TextView"
        android:textColor="#ffffff"
        android:textSize="25sp" />
</LinearLayout>
  • 上述代码效果:
    4.png
  • 可见,上面两种组合是生效的。
  • 通过实际操作,我们得出一个基本结论:
    1. 当父布局的属性android:orientation="horizontal",其子控件属性:android:layout_gravity=在水平方向将失效,而竖直方向不受影响,继续起作用。
    2. 当父布局的属性android:orientation="vertical",其子控件属性:android:layout_gravity=在竖直方向将失效,而水平方向不受影响,将继续起作用。
  • 结论倒是有了:简单来说,如果父布局规定了其子控件按照某一方向进行,那么子控件在该方向上调整自己位置的能力将消失。不过为什么会这样呢,我想有如下考虑:
  1. 两个方向便能确定子控件在父控件的具体位置,父控件确定一个维度,子控件本事确定一个维度,逻辑清晰明了。
  2. 最为关键是,如果父控件和子控件都能在同一维度起作用的话,会有明显的冲突,比如布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_one"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/tv_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="#0000ff"
        android:text="第一个TextView"
        android:textColor="#ffffff"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tv_tow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#555555"
        android:text="第二个TextView"
        android:textColor="#ffffff"
        android:textSize="25sp" />
</LinearLayout>
  • 该布局效果:
    5.png
  • 按照逻辑,父布局设置了其子控件按照从上到下的序列排列,如果此时子控件还具有该方向上调整自身位置的能力,一旦某个控件做出调整要求,如“”第一个TextView“”想把自己排列到屏幕底部,那么其他控件如何处之?“第一个TextView”可是应该排在他们前面的啊,那么他们只能往下排,排到屏幕外了?这就是问题所在,又比如“第二个控件”想把自己排到屏幕最上方,也会有相应问题:与之相反的是,此时无论“第一个TextView”还是“第二个TextView”在左右(水平)方向上调整位置,都不会对其他控件产生影响(只要你还是在我前面或者后面,至于是左前方,右前方,还是正前方,都没有与父布局规定相冲突,这就可以了)。所以,个人认为,正是基于这样显而易见冲突的考虑,当子控件试图在父布局已经规定排列方式的方向上调整自己的位置时,调整位置的属性将会自动失效
    这样,我们便对属性android:orientation和属性android:layout_gravity有了进一步认识。
  • 那么就如最后的代码,我们在父布局已经是竖直方向的情况下,我们究竟能不能通过某种手段,将“第二个TextView”放到屏幕底部呢,之前我们通过在父布局设置android:gravity到达过目的,不过这次,我们有了新的需求了:第一个TextView在屏幕顶部,第二个在屏幕底部。下一篇我们讲讲如何实现新的需求。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,290评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,399评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,021评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,034评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,412评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,651评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,902评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,605评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,339评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,586评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,076评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,400评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,060评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,851评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,685评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,595评论 2 270

推荐阅读更多精彩内容