LinearLayout中的TextView按比例顯示和文本框寬度隨輸入字符改變

首先看一下Layout_weight屬性的做用:它是用來分配屬於空間的一個屬性,你能夠設置他的權重。不少人不知道剩餘空間是個什麼概念,下面我先來講說剩餘空間。android

看下面代碼:spa

1.  <?xml version="1.0" encoding="utf-8"?>     code

2.  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xml

3.      android:orientation="vertical"     three

4.      android:layout_width="fill_parent"     utf-8

5.      android:layout_height="fill_parent"     ci

6.      >     it

7.  <EditText     io

8.      android:layout_width="fill_parent"     ast

9.      android:layout_height="wrap_content"     

10.     android:gravity="left"     

11.     android:text="one"/>     

12. <EditText     

13.     android:layout_width="fill_parent"     

14.     android:layout_height="wrap_content"     

15.     android:gravity="center"     

16.     android:layout_weight="1.0"     

17.     android:text="two"/>     

18.     <EditText     

19.     android:layout_width="fill_parent"     

20.     android:layout_height="wrap_content"     

21.     android:gravity="right"     

22.     android:text="three"/>     

23. </LinearLayout>     

運行結果是:

看上面代碼:只有Button2使用了Layout_weight屬性,並賦值爲了1,而Button1Button3沒有設置Layout_weight這個屬性,根據API,可知,他們默認是0

下面我就來說,Layout_weight這個屬性的真正的意思:Android系統先按照你設置的3Button高度Layout_heightwrap_content,給你分配好他們3個的高度,

而後會把剩下來的屏幕空間所有賦給Button2,由於只有他的權重值是1,這也是爲何Button2佔了那麼大的一塊空間。

有了以上的理解咱們就能夠對網上關於Layout_weight這個屬性更讓人費解的效果有一個清晰的認識了。

咱們來看這段代碼:

1.   <xml version="1.0" encoding="UTF-8">   

2.  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   

3.      android:layout_width="fill_parent"   

4.      android:layout_height="wrap_content"   

5.      android:orientation="horizontal" >   

6.      <TextView   

7.          android:background="ff0000"   

8.          android:layout_width="**"   

9.          android:layout_height="wrap_content"   

10.         android:text="1"   

11.         android:textColor="android:color/white"   

12.         android:layout_weight="1"/>   

13.     <TextView   

14.         android:background="cccccc"   

15.         android:layout_width="**"   

16.         android:layout_height="wrap_content"   

17.         android:text="2"   

18.         android:textColor="android:color/black"   

19.         android:layout_weight="2" />   

20.      <TextView   

21.         android:background="ddaacc"   

22.         android:layout_width="**"   

23.         android:layout_height="wrap_content"   

24.         android:text="3"   

25.         android:textColor="android:color/black"   

26.         android:layout_weight="3" />   

27. </LinearLayout> 

三個文本框的都是 layout_width=wrap_content 時,會獲得如下效果


按照上面的理解,系統先給3TextView分配他們的寬度值wrap_content(寬度足以包含他們的內容1,2,3便可),而後會把剩下來的屏幕空間按照1:2:3的比列分配給3textview,因此就出現了上面的圖像。

而當layout_width=fill_parent時,若是分別給三個TextView設置他們的Layout_weight122的話,就會出現下面的效果:


你會發現1的權重小,反而分的多了,這是爲何呢???網上不少人說是當layout_width=fill_parent時,weighth值越小權重越大,優先級越高,就好像在背口訣

同樣,其實他們並無真正理解這個問題,真正的緣由是Layout_width="fill_parent"的緣由形成的。依照上面理解咱們來分析:

系統先給3textview分配他們所要的寬度fill_parent,也就是說每一都是填滿他的父控件,這裏就死屏幕的寬度

那麼這時候的剩餘空間=1parent_width-3parent_width=-2parent_width (parent_width指的是屏幕寬度 )

那麼第一個TextView的實際所佔寬度應該=fill_parent的寬度,parent_width + 他所佔剩餘空間的權重比列1/5 * 剩餘空間大小(-2 parent_width=3/5parent_width

同理第二個TextView的實際所佔寬度=parent_width + 2/5*(-2parent_width)=1/5parent_width;

第三個TextView的實際所佔寬度=parent_width + 2/5*(-2parent_width)=1/5parent_width;因此就是3:1:1的比列顯示了。

這樣你也就會明白爲何當你把三個Layout_weight設置爲123的話,會出現下面的效果了:

第三個直接不顯示了,爲何呢?一塊兒來按上面方法算一下吧:

系統先給3textview分配他們所要的寬度fill_parent,也就是說每一都是填滿他的父控件,這裏就死屏幕的寬度

那麼這時候的剩餘空間=1parent_width-3parent_width=-2parent_width (parent_width指的是屏幕寬度 )

那麼第一個TextView的實際所佔寬度應該=fill_parent的寬度,parent_width + 他所佔剩餘空間的權重比列1/6 * 剩餘空間大小(-2 parent_width=2/3parent_width

同理第二個TextView的實際所佔寬度=parent_width + 2/6*(-2parent_width)=1/3parent_width;

第三個TextView的實際所佔寬度=parent_width + 3/6*(-2parent_width)=0parent_width;因此就是2:1:0的比列顯示了。第三個就直接沒有空間了。

 第二個問題

(1)文本框寬度隨輸入字符改變,設置layout_width設置爲"wrap_content"若是爲豎直方向的設置android:layout_height="wrap_content"

<LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:orientation="horizontal"

        android:layout_weight="1">

 

<TextView

    android:layout_width="wrap_content"

    android:layout_height="fill_parent"

    android:layout_gravity="center"

    android:layout_weight="1"

    android:background="#0000FF"

    android:text="深藍fdhgdhfghfgjghjkgjkg" />

 

<TextView

    android:layout_width="wrap_content"

    android:layout_height="fill_parent"

    android:layout_gravity="center"

    android:layout_weight="1"

    android:background="#FF0000"

    android:text="" />

 

<TextView

    android:layout_width="wrap_content"

    android:layout_height="fill_parent"

    android:layout_gravity="center"

    android:layout_weight="1"

    android:background="#4B0082"

    android:text="" />

 

<TextView

    android:layout_width="wrap_content"

    android:layout_height="fill_parent"

    android:layout_gravity="center"

    android:layout_weight="1"

    android:background="#00BFFF"

    android:text="淺藍" />

 

 </LinearLayout>

 

2(1)文本框寬度不隨輸入字符改變,設置layout_width設置爲"0dp"若是爲豎直方向的設置android:layout_height="0dp"

<LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:orientation="horizontal"

        android:layout_weight="1">

 

<TextView

    android:layout_width="0dp"

    android:layout_height="fill_parent"

    android:layout_weight="1"

    android:text="深藍sdgdfhgfhfgjh"

    android:background="#0000FF" />

 

<TextView

    android:layout_width="0dp"

    android:layout_height="fill_parent"

    android:layout_weight="1"

    android:text=""

    android:background="#FF0000"/>

<TextView

    android:layout_width="0dp"

    android:layout_height="fill_parent"

    android:layout_weight="1"

    android:text=""

    android:background="#4B0082"/>

<TextView

    android:layout_width="0dp"

    android:layout_height="fill_parent"

    android:layout_weight="1"

    android:text="淺藍"

    android:background="#00BFFF"/>

 </LinearLayout>

相關文章
相關標籤/搜索