欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

android之layout_weight體驗(實現(xiàn)按比例顯示)

系統(tǒng) 1908 0
在android開發(fā)中LinearLayout很常用,LinearLayout的內(nèi)控件的android:layout_weight在某些場景顯得非常重要,比如我們需要按比例顯示。android并沒用提供table這樣的控件,雖然有TableLayout,但是它并非是我們想象中的像html里面的table那么好用,我們常用ListView實現(xiàn)table的效果,但是列對齊確比較麻煩,現(xiàn)在用LinearLayout及屬性android:layout_weight能很好地解決。下面我們共同體驗下layout_weight這個屬性。

  一、LinearLayout內(nèi)的控件的layout_width設(shè)置為"wrap_content",請看一下xml配置:

      
        <
      
      
        LinearLayout
      
      
      
        android:orientation
      
      
        ="horizontal"
      
      
        
      android:layout_width
      
      
        ="fill_parent"
      
      
        
      android:layout_height
      
      
        ="fill_parent"
      
      
        
      android:layout_weight
      
      
        ="1"
      
      
      
      
        >
      
      
        <
      
      
        TextView
          
      
      
        android:layout_width
      
      
        ="wrap_content"
      
      
        
          android:layout_height
      
      
        ="fill_parent"
      
      
        
          android:layout_weight
      
      
        ="1"
      
      
        
          android:background
      
      
        ="#aa0000"
      
      
        
          android:gravity
      
      
        ="center"
      
      
        
          android:text
      
      
        ="1"
      
      
        />
      
      
        <
      
      
        TextView
          
      
      
        android:layout_width
      
      
        ="wrap_content"
      
      
        
          android:layout_height
      
      
        ="fill_parent"
      
      
        
          android:layout_weight
      
      
        ="2"
      
      
        
          android:background
      
      
        ="#00aa00"
      
      
        
          android:gravity
      
      
        ="center"
      
      
        
          android:text
      
      
        ="1"
      
      
        />
      
      
        <
      
      
        TextView
          
      
      
        android:layout_width
      
      
        ="wrap_content"
      
      
        
          android:layout_height
      
      
        ="fill_parent"
      
      
        
          android:layout_weight
      
      
        ="3"
      
      
        
          android:background
      
      
        ="#0000aa"
      
      
        
          android:gravity
      
      
        ="center"
      
      
        
          android:text
      
      
        ="1"
      
      
        />
      
      
        </
      
      
        LinearLayout
      
      
        >
      
    

效果如下:

android之layout_weight體驗(實現(xiàn)按比例顯示)

可以看到這三個TextView是按照1:2:3的比例進(jìn)行顯示的,這樣看來似乎可以實現(xiàn)按照比例顯示了,但是有個問題,如果TextView內(nèi)的文本長度一同那么較長文本的TextView會寬度會有所增加,見下面配置及效果:

配置:

      
        <
      
      
        LinearLayout
      
      
      
        android:orientation
      
      
        ="horizontal"
      
      
        
      android:layout_width
      
      
        ="fill_parent"
      
      
        
      android:layout_height
      
      
        ="fill_parent"
      
      
        
      android:layout_weight
      
      
        ="1"
      
      
        >
      
      
        <
      
      
        TextView
          
      
      
        android:layout_width
      
      
        ="wrap_content"
      
      
        
          android:layout_height
      
      
        ="fill_parent"
      
      
        
          android:layout_weight
      
      
        ="1"
      
      
        
          android:background
      
      
        ="#aa0000"
      
      
        
          android:gravity
      
      
        ="center"
      
      
        
          android:text
      
      
        ="1111111111111111111111111111111111111111111"
      
      
        />
      
      
        <
      
      
        TextView
          
      
      
        android:layout_width
      
      
        ="wrap_content"
      
      
        
          android:layout_height
      
      
        ="fill_parent"
      
      
        
          android:layout_weight
      
      
        ="2"
      
      
        
          android:background
      
      
        ="#00aa00"
      
      
        
          android:gravity
      
      
        ="center"
      
      
        
          android:text
      
      
        ="2"
      
      
        />
      
      
        <
      
      
        TextView
          
      
      
        android:layout_width
      
      
        ="wrap_content"
      
      
        
          android:layout_height
      
      
        ="fill_parent"
      
      
        
          android:layout_weight
      
      
        ="3"
      
      
        
          android:background
      
      
        ="#0000aa"
      
      
        
          android:gravity
      
      
        ="center"
      
      
        
          android:text
      
      
        ="3"
      
      
        />
      
      
        </
      
      
        LinearLayout
      
      
        >
      
    

效果:

android之layout_weight體驗(實現(xiàn)按比例顯示)

這樣看來我們所需要的按比例又無法實現(xiàn)了,經(jīng)過滿天地google終于找到了解決方案,就是設(shè)置layout_width設(shè)置為"wrap_content"。配置及效果見下:

      
        <
      
      
        LinearLayout
      
      
      
        android:orientation
      
      
        ="horizontal"
      
      
        
      android:layout_width
      
      
        ="fill_parent"
      
      
        
      android:layout_height
      
      
        ="fill_parent"
      
      
        
      android:layout_weight
      
      
        ="1"
      
      
        >
      
      
        <
      
      
        TextView
          
      
      
        android:layout_width
      
      
        ="0dp"
      
      
        
          android:layout_height
      
      
        ="fill_parent"
      
      
        
          android:layout_weight
      
      
        ="1"
      
      
        
          android:background
      
      
        ="#aa0000"
      
      
        
          android:gravity
      
      
        ="center"
      
      
        
          android:text
      
      
        ="1111111111111111111111111111111111111111111"
      
      
        />
      
      
        <
      
      
        TextView
          
      
      
        android:layout_width
      
      
        ="0dp"
      
      
        
          android:layout_height
      
      
        ="fill_parent"
      
      
        
          android:layout_weight
      
      
        ="2"
      
      
        
          android:background
      
      
        ="#00aa00"
      
      
        
          android:gravity
      
      
        ="center"
      
      
        
          android:text
      
      
        ="2"
      
      
        />
      
      
        <
      
      
        TextView
          
      
      
        android:layout_width
      
      
        ="0dp"
      
      
        
          android:layout_height
      
      
        ="fill_parent"
      
      
        
          android:layout_weight
      
      
        ="3"
      
      
        
          android:background
      
      
        ="#0000aa"
      
      
        
          android:gravity
      
      
        ="center"
      
      
        
          android:text
      
      
        ="3"
      
      
        />
      
      
        </
      
      
        LinearLayout
      
      
        >
      
    

效果:

android之layout_weight體驗(實現(xiàn)按比例顯示)

這樣終于達(dá)到我們的按比例顯示的效果了,感覺很是奇怪,android開發(fā)框架的大佬們有時候設(shè)計確實有點匪夷所思。

  二、LinearLayout內(nèi)的控件的layout_width設(shè)置為"fill_parent",請看一下xml配置:

      
        <
      
      
        LinearLayout
      
      
      
        android:orientation
      
      
        ="horizontal"
      
      
        
      android:layout_width
      
      
        ="fill_parent"
      
      
        
      android:layout_height
      
      
        ="fill_parent"
      
      
        
      android:layout_weight
      
      
        ="1"
      
      
        >
      
      
        <
      
      
        TextView
          
      
      
        android:layout_width
      
      
        ="fill_parent"
      
      
        
          android:layout_height
      
      
        ="fill_parent"
      
      
        
          android:layout_weight
      
      
        ="1"
      
      
        
          android:background
      
      
        ="#aa0000"
      
      
        
          android:gravity
      
      
        ="center"
      
      
        
          android:text
      
      
        ="1"
      
      
        />
      
      
        <
      
      
        TextView
          
      
      
        android:layout_width
      
      
        ="fill_parent"
      
      
        
          android:layout_height
      
      
        ="fill_parent"
      
      
        
          android:layout_weight
      
      
        ="2"
      
      
        
          android:background
      
      
        ="#00aa00"
      
      
        
          android:gravity
      
      
        ="center"
      
      
        
          android:text
      
      
        ="2"
      
      
        />
      
      
        </
      
      
        LinearLayout
      
      
        >
      
    


效果如下:

奇怪吧,整個寬度平分3塊,第一個TextView占了兩塊,這樣看來weight值越小的優(yōu)先級越大。只有兩個TextView似乎看出些道理,那么讓我們看看三個是什么效果:

      
        <
      
      
        LinearLayout
      
      
      
        android:orientation
      
      
        ="horizontal"
      
      
        
      android:layout_width
      
      
        ="fill_parent"
      
      
        
      android:layout_height
      
      
        ="fill_parent"
      
      
        
      android:layout_weight
      
      
        ="1"
      
      
        >
      
      
        <
      
      
        TextView
          
      
      
        android:layout_width
      
      
        ="fill_parent"
      
      
        
          android:layout_height
      
      
        ="fill_parent"
      
      
        
          android:layout_weight
      
      
        ="1"
      
      
        
          android:background
      
      
        ="#aa0000"
      
      
        
          android:gravity
      
      
        ="center"
      
      
        
          android:text
      
      
        ="1"
      
      
        />
      
      
        <
      
      
        TextView
          
      
      
        android:layout_width
      
      
        ="fill_parent"
      
      
        
          android:layout_height
      
      
        ="fill_parent"
      
      
        
          android:layout_weight
      
      
        ="2"
      
      
        
          android:background
      
      
        ="#00aa00"
      
      
        
          android:gravity
      
      
        ="center"
      
      
        
          android:text
      
      
        ="2"
      
      
        />
      
      
        <
      
      
        TextView
          
      
      
        android:layout_width
      
      
        ="fill_parent"
      
      
        
          android:layout_height
      
      
        ="fill_parent"
      
      
        
          android:layout_weight
      
      
        ="3"
      
      
        
          android:background
      
      
        ="#0000aa"
      
      
        
          android:gravity
      
      
        ="center"
      
      
        
          android:text
      
      
        ="3"
      
      
        />
      
      
        </
      
      
        LinearLayout
      
      
        >
      
    

效果:

什么意思?第三個TextView丟掉了,很是奇怪,讓我們再試一個,把weight分別改為2,3,4的看看效果:

這個效果讓人很困惑,我一直想尋求出一個確切的比例公式,但是至今未找到。有哪位大神能搞定的話忘不吝賜教。

雖然這個android:layout_weight屬性很怪異,但幸運的是我們達(dá)到了目標(biāo):

  按比例顯示LinearLayout內(nèi)各個子控件,需設(shè)置android:layout_width="0dp",如果為豎直方向的設(shè)置android:layout_height="0dp"。在這種情況下某子個控件占用LinearLayout的比例為:本控件weight值 / LinearLayout內(nèi)所有控件的weight值的和。

android之layout_weight體驗(實現(xiàn)按比例顯示)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日韩国产欧美在线观看一区二区 | 天天夜夜人人 | 日韩黄色一级大片 | 四虎福利 | 性一级录像片片视频免费看 | 久久久久久久一区二区三区 | 青娱在线| 亚洲性生活免费视频 | 日本在线小视频 | 欧美日韩视频 | 色综合久久综精品 | 在线视频一区二区 | 日本精品在线观看 | 亚洲高清一区二区三区 | 亚洲欧美另类色妞网站 | 午夜精品在线播放 | 国产精品久久久久久免费软件 | 视频一区二区三区四区五区 | 97婷婷色| 国产成人精品一区二区仙踪林 | 免费播放春色aⅴ视频 | 无码激情做A爰片毛片A片小说 | 日本在线观看高清不卡免v 国产成人一区二区精品非洲 | 一区二区三区在线电影 | 中文字幕在线一区二区三区 | 日韩在线毛片 | 成年人免费网站在线观看 | a欧美 | 色狠狠色狠狠综合天天 | 日韩欧美三级在线 | 国产成人高清视频 | a在线免费观看 | 国产精品国产三级国产aⅴ 精品视频在线播放 | 2019天天干天天操 | 一级片a| 成人在线视频免费观看 | 精品综合 | 精品91久久 | 欧美精品一区三区 | www亚洲成人| 欧美日韩在线观看视频 |