About the three attributes of RelativeLayout in Android: layout_centerVertical, layout_centerInParent, layout_centerHorizontal

Today, when looking at the page code of the Sina Weibo client written by a great god on the Internet, I saw it in a relativelayout


 android:layout_centerVertical="true" this paragraph


So the role played by this code has made me interested, and the focus of attention is: the result of this code.

When I was looking at the xml view page of the eclipse development machine, I found that after this sentence, a green line appeared in the corresponding RelativeLayout. My intuitive feeling is that this sentence should be applied to this RelativeLayout. Of the control.

But with a scientific attitude, I checked again and found that there was very little information on the Internet. In the end, I resorted to a killer trick: practice it myself!

The result was beyond my expectation:

In fact, the function of this sentence is: Let this relative layout be in the center of the vertical direction of its parent control. It does not specify that its internal controls are at the center of the vertical direction

I read an article that is more powerful before: http://blog.csdn.net/softkexin/article/details/5933589 

This article is about the role of some attributes in RelativeLayout

The last three sentences:"

android:layout_centerInParent="true" --Place the control in the center of the parent control
android:layout_centerHorizontal="true" --Place the control in the center of the horizontal direction
android:layout_centerVertical="true" --Place the control in the vertical The center of the direction

The description of the last two of these three sentences should be changed to:

android: layout_centerHorizontal = "true" - The present control is placed on the parent control the horizontal center position
android: layout_centerVertical = "true" - The present control is placed on the parent control vertical center position

Here is a screenshot of the effect of android:layout_centerVertical="true" :


Corresponding code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        <span style="color:#ff0000;">android:layout_centerVertical="true"</span> >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="helloworld" />
    </RelativeLayout>

</RelativeLayout>
Below is a screenshot of the android:layout_centerHorizontal ="true" effect:

Corresponding code:

<span style="color:#333333;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        </span><span style="color:#ff0000;">android:layout_centerHorizontal="true" </span><span style="color:#333333;">>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="helloworld" />
    </RelativeLayout>

</RelativeLayout></span>

As for android:layout_centerInParent="true", this attribute is actually equivalent to combining the above two attributes, which has the same effect as specifying the first two as true at the same time.

One more thing needs to be added: these properties will only appear when the parent control is RelativeLayout, that is to say, the child control will have these three properties when the parent control is a relative layout.

Regardless of the layout of the child control, TextView or Button, it will have these properties.

At this time, the RelativeLayout itself, which is the parent control, does not have these properties. In other words, once a control can specify these three properties, then it should be a child control of a RelativeLayout. Of course, this is only the conclusion I have drawn so far. It is not very clear whether it is correct or not, but these are the conclusions I have drawn based on practice at present, at least for the time being, it can be used as a reference.

    Finally, let me talk about my thoughts:

            学习这个东西,真的是要多思考,这样才能触类旁通,还要有钻研的精神,实事求是的态度,我之所以会对这个属性的显示结果感到好奇,也有很大一部分原因,在于我不久前研究了android:gravity与android:layout_gravity的区别,gravity是作用于这个控件的子控件的;而layout_gravity则是作用于使用这个属性的控件本身的,它是指定让自己处于父控件的什么位置上(靠左,靠右,居中);当我看见android:layout_centerVertical=true这个属性出现在的控件是一个RelativeLayout中时,我的第一反应就是这个属性是作用在子控件上的,然后我又去视图编辑器里面看,发现有了这个属性相对布局本身中间多了一条绿色的横线。就主观来推测的话,至少我是认为,这个横线说明这个布局里面的控件排列遵循这条横线。这样就得出了有了这个属性,布局内部控件要在竖直方向上居中的结论,但是我想到:layout_gravity不是作用于子控件的,而是作用于自身的,指定自身相对于父控件的位置,这样的话,让我直接联想到的就是:带有"layout_"这样的属性,应该都是作用于自身才对,有的朋友可能要问了:为什么要作用于自身?

其实我想到的原因很简单,google的设计人员,跟我们一样,也喜欢简单易用的原则,那么如果带有"layout_"这样名字的属性,既有作用于子控件的,又有作用于自身的,那样的话,使用起来感觉很混乱,而且也不方便编程人员的记忆,从这个角度讲android布局本身的易用性也会打折扣,作为全世界最优秀的软件设计人员,不可能犯这么二的错误吧~

      所以我就抱着试试看的态度,进行了这两个属性的试验,结果果然跟自己的第一印象不同。也验证了我的另一个推测:带有"layout_"字样的属性,一般情况下都是作用于自身的。

    总结一下我一开始的话,学习这个东西真的要多思考(比如知道了android:gravity与android:layout_gravity的作用范围区别之后,看到带有"layout_"字样的属性的时候就要注意,与不带"layout_"的同名属性之间的区别是不是跟gravity的情况类似),还要有钻研精神(比如在网上找了很多的资料,但是没有一个讲的特别清楚的情况下,要做好自己调查的思想准备,因为这样才能真正掌握),实事求是的态度(比如我在这个例子中的表现,一开始就先入为主的感觉作用范围是什么,这样的推测的方式不能说不好,因为很多时候我们的推测也都是有依据的,很多时候也会做出正确的推测,但是之后要有一个实事求是的态度去看待,不能自己推测之后就主观臆断,不能怕麻烦,怕动手浪费时间,其实自己去求真的结果很可能是你的做法不仅没有浪费时间,而且还为以后节省了很多的时间)。

   好了,废话一大堆,希望这篇文章可以帮到一些也遇到同样问题的朋友~谢谢~~^o^



Guess you like

Origin blog.csdn.net/awy1988/article/details/40341995
Recommended