Android的LinearLayout水平一行使控件居右对齐

2014年07月31日 Android 18条评论 阅读54664次

Android的LinearLayout水平一行使控件居右对齐

本文来源自极乐鸟博客  2014年7月31日        

上海  天气热        

有这样一个场景:Android的listview中每一行因为都要写个单独的针对这一行item的layout文件,而这一行数据的布局需求是这样的,一个图片,一个文字显示,一个checkbox复选框。
其中图片大小是固定的,而每行的文本长度确实不一样的。
基于这样的一个布局需求,我们为其设计,必然左侧的图片都是要居左对齐的,其后的可变的文本,最后的checkbox复选框显然必须得居右对齐。

但就是在这样一个简单的页面布局上,我确花费了2小时之多的时间来完成,当然最后还是在网络资源的帮助下完成的。

具体思路是:
将最右边的的checkbox复选框控件单独放在另一个LinearLayout中,同时将其对齐方式设为右对齐:android:gravity="right",还有一点,这个LinearLayout的宽度设为充满父控件: android:layout_width="fill_parent"。

完整的XML代码如下:


<?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="horizontal" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="可变要显示的文字" />

    <!-- 下面到关键部分:
        将最右边的的checkbox复选框控件单独放在另一个LinearLayout中
        同时将其对齐方式设为右对齐:android:gravity="right"
        同时将LinearLayout的宽度设为充满父控件: android:layout_width="fill_parent"
     -->
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right" >

        <CheckBox
            <CheckBox
            android:id="@+id/item_cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false" />
    </LinearLayout>

</LinearLayout>


至此,完整的完成本文开发需求的布局文件就写好了。
不过最后还是留下了疑问了,不知道为什么一定要这样子来实现,而我最初用的android:layout_gravity="right"确不起作用。

分享本文至:

WRITTEN BY

avatar
本文标签:androidlinearLayout
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

18 条评论

  1. <a href="http://www.goseecreate.com/nike-air-max-bw-ultra-se-herren-schuh-polish-kurpesb">nike air max bw ultra se herren schuh polish</a> <a href="http://www.klarinettino.com/nike-cortez-gold-zone-scarpef">nike cortez gold zone</a> <a href="http://www.loydartist.com/hermes-evelyne-29-price-journal-hermesa">hermes evelyne 29 price journal</a> <a href="http://www.musicbreeds.com/nike-air-presto-anti-fur-lyser%C3%B8d-brun-chaussuresa">nike air presto anti fur lyser酶d brun</a> <a href="http://www.plakglas.com/nike-roshe-woman-tumblr-tumblr-couple-shoese">nike roshe woman tumblr tumblr couple</a> <a href="http://www.samuelzide.com/ugg-classic-short-metallic-boots-india-uggsb">ugg classic short metallic boots india</a>
    mbesthetics http://www.mbesthetics.com/

  2. <a href="http://www.goodmedusa.com/nike-air-max-1-em-men-red-black-white-shoesi">nike air max 1 em men red black white</a> <a href="http://www.murskacorp.com/2013-nike-free-run-5.0-femminile-nero-grigio-scarpej">2013 nike free run 5.0 femminile nero grigio</a> <a href="http://www.prajansim.com/air-jordan-melo-m11-schwarz-gr%C3%BCn-schuhej">air jordan melo m11 schwarz gr眉n</a> <a href="http://www.pokersurga.com/pittsburgh-panthers-47-ncaa-double-out-47-clean-up-cap-hath">pittsburgh panthers 47 ncaa double out 47 clean up cap</a> <a href="http://www.arkmmj.com/dark-gris-converse-hommes-chaussuresh">dark gris converse hommes</a> <a href="http://www.butterbroad.com/nike-zoom-winflo-3-blau-schuheh">nike zoom winflo 3 blau</a>
    twistnik http://www.twistnik.com/

  3. <a href="http://www.interfaithstewards.net/nike-free-run-salg-kvinners-hack-butya">nike free run salg kvinners hack</a> <a href="http://www.inwardoutward.net/nike-air-max-90-em-gr%C3%BCn-silber-schuheg">nike air max 90 em gr眉n silber</a> <a href="http://www.isabellenhuette.net/herren-nike-free-5.0-blau-rot-butyf">herren nike free 5.0 blau rot</a> <a href="http://www.jeffersonshine.net/nike-kobe-10-marine-bleu-blanc-rouge-butya">nike kobe 10 marine bleu blanc rouge</a> <a href="http://www.jerfsa.net/kvinners-nike-kobe-9-low-oransje-gr%C3%A5-obuvb">kvinners nike kobe 9 low oransje gr氓</a> <a href="http://www.jgeinc.net/nike-hyperdunk-2015-mid-noir-blanc-scarped">nike hyperdunk 2015 mid noir blanc</a>
    michalowsky http://www.michalowsky.net/

  4. <a href="http://www.dvdlivedoor.com/mujer-air-jordan-retro-14-blanco-negro-zapatosj">mujer air jordan retro 14 blanco negro</a> <a href="http://www.exterminas.com/herren-salomon-xt-wings-3-himmelblau-chaussuresa">herren salomon xt wings 3 himmelblau</a> <a href="http://www.filoliteafarm.com/nike-sb-stefan-janoski-max-dam%C3%A4nner-rosa-rot-foota">nike sb stefan janoski max dam盲nner rosa rot</a> <a href="http://www.shalandaleigh.com/nike-air-presto-mid-rojo-negro-runningd">nike air presto mid rojo negro</a> <a href="http://www.skepticmoment.com/mlb-jerseys-colorado-rockies-17-todd-helton-black-fashion-jerseys-nflh">mlb jerseys colorado rockies 17 todd helton black fashion jerseys</a> <a href="http://www.kleankampsite.com/nike-huarache-ultra-naranja-zapatosh">nike huarache ultra naranja</a>
    innordicau http://www.innordicau.com/

  5. <a href="http://www.mwartsdrama.com/bryce-harper-signed-jersey-nfli">bryce harper signed jersey</a> <a href="http://www.meshmotors.com/ua-curry-3-r%C3%B8d-hvit-obuvp">ua curry 3 r酶d hvit</a> <a href="http://www.aspenmedisys.com/coach-legacy-stripe-in-signature-large-black-wallets-coachh">coach legacy stripe in signature large black wallets</a> <a href="http://www.smartiesland.com/nike-lunar-force-1-sky-hi-nero-trainers-scarpei">nike lunar force 1 sky hi nero trainers</a> <a href="http://www.emcetolerie.com/air-jordan-retro-12-gamma-blu-bianca-scarpeh">air jordan retro 12 gamma blu bianca</a> <a href="http://www.lapaktoko.com/new-york-rangers-snapback-mitchell-and-ness-hath">new york rangers snapback mitchell and ness</a>
    isabellerigal http://www.isabellerigal.com/

  6. <a href="http://www.rondamice.com/asics-gel-noosa-tri-8-gul-bl%C3%A5-chaussuresa">asics gel noosa tri 8 gul bl氓</a> <a href="http://www.neuroassocpc.com/nike-roshe-run-speckle-negro-foota">nike roshe run speckle negro</a> <a href="http://www.rohanchougule.com/nike-air-huarache-argento-verde-runningd">nike air huarache argento verde</a> <a href="http://www.tgpmusic.com/image-1-shawn-kemp-autographed-cleveland-cavs-adult-48-nba-starter-jersey-nflh">image 1 shawn kemp autographed cleveland cavs adult 48 nba starter jersey</a> <a href="http://www.bigcbigcard.com/adidas-gazelle-blanco-zapatosh">adidas gazelle blanco</a> <a href="http://www.jbsbruxelles.com/kd-trey-5-grau-and-orange-gelb-scarpea">kd trey 5 grau and orange gelb</a>
    chotinaukri http://www.chotinaukri.com/

  7. <a href="http://www.koreazp.com/femminile-asics-gel-kayano-20-grigio-blu-scarpei">femminile asics gel kayano 20 grigio blu</a> <a href="http://www.hiltonskyshop.com/nike-air-max-95-femminile-viola-grigio-scarpeh">nike air max 95 femminile viola grigio</a> <a href="http://www.lavaforchange.com/milwaukee-brewers-new-era-mlb-heather-black-white-59fifty-cap-hath">milwaukee brewers new era mlb heather black white 59fifty cap</a> <a href="http://www.lifeonfilms.com/nike-free-run-3-hommes-bleu-chaussuresh">nike free run 3 hommes bleu</a> <a href="http://www.midlandroofer.com/hommes-nike-free-run-2-bleu-vert-chaussuresj">hommes nike free run 2 bleu vert</a> <a href="http://www.pearsonchool.com/nike-zoom-pegasus-33-dam%C3%A4nner-rot-himmelblau-schuhej">nike zoom pegasus 33 dam盲nner rot himmelblau</a>
    vanltl http://www.vanltl.com/

  8. avatar dkstekstil

    <a href="http://www.mumbaikichut.com/under-armour-curry-1-low-negro-rosado-zapatosl">under armour curry 1 low negro rosado</a> <a href="http://www.ikmsdkravmaga.com/herre-nike-lunarglide-6-hvid-gul-skoj">herre nike lunarglide 6 hvid gul</a> <a href="http://www.themusinglens.com/mennns-air-max-90-hyperfuse-r%C3%B8d-rosa-obuvi">mennns air max 90 hyperfuse r酶d rosa</a> <a href="http://www.startagencia.com/nike-kobe-7-rosado-naranja-zapatosp">nike kobe 7 rosado naranja</a> <a href="http://www.drjennieward.com/nike-kobe-8-gr%C3%B8n-lilla-skol">nike kobe 8 gr酶n lilla</a> <a href="http://www.genievres.com/air-jordan-retro-9-femminile-oro-viola-scarpeh">air jordan retro 9 femminile oro viola</a>
    dkstekstil http://www.dkstekstil.com/

  9. avatar emissariesbc

    <a href="http://www.taiwanmeinet.com/michael-kors-mini-selma-bag-mkh">michael kors mini selma bag</a> <a href="http://www.talkwithgirl.com/kvinners-nike-free-flyknit-4.0-gr%C3%A5-hvit-obuvi">kvinners nike free flyknit 4.0 gr氓 hvit</a> <a href="http://www.mamatale.com/adidas-samba-super-nero-scarpek">adidas samba super nero</a> <a href="http://www.strongtale.com/womens-nike-kobe-12-grey-shoesi">womens nike kobe 12 grey</a> <a href="http://www.iranpayamco.com/nike-sb-zoom-stefan-janoski-blanco-cuero-skate-zapatos-zapatosi">nike sb zoom stefan janoski blanco cuero skate zapatos</a> <a href="http://www.hapeid.com/herre-nike-air-vapormax-plus-gul-skoh">herre nike air vapormax plus gul</a>
    emissariesbc http://www.emissariesbc.com/

  10. avatar askedwhite

    <a href="http://www.ajpaircon.com/nike-free-trainer-all-svart-obuvi">nike free trainer all svart</a> <a href="http://www.eraoyun.com/p%C3%BArpura-zest-negro-ua-clutchfit-drive-3-low-zapatosl">p煤rpura zest negro ua clutchfit drive 3 low</a> <a href="http://www.kembaradunia.com/cheap-oakley-deviation-sunglasses-sunglassesh">cheap oakley deviation sunglasses</a> <a href="http://www.pybursa.com/cleveland-indians-new-era-mlb-shade-classic-neo-39thirty-cap-hatj">cleveland indians new era mlb shade classic neo 39thirty cap</a> <a href="http://www.astonsunoco.com/nike-cortez-maschio-grigio-viola-scarpej">nike cortez maschio grigio viola</a> <a href="http://www.mcguber.com/air-jordan-horizon-wolf-grau-schuhei">air jordan horizon wolf grau</a>
    askedwhite http://www.askedwhite.com/

  11. avatar juliemobley

    <a href="http://www.truenthusiast.com/billig-nike-air-vapormax-dam%C3%A4nner-silber-shoesa">billig nike air vapormax dam盲nner silber</a> <a href="http://www.nrthofhere.com/nike-air-jordan-schuhe-skos">nike air jordan schuhe</a> <a href="http://www.ksjfifgmif.com/versace-mens-medusa-belt-beltr">versace mens medusa belt</a> <a href="http://www.gbwpvqyg.com/mackage-bag-all-virgin-mackagec">mackage bag all virgin</a> <a href="http://www.alannacaldas.com/le-coq-sportif-sneakers-south-africa-skow">le coq sportif sneakers south africa</a> <a href="http://www.lmbaidu.com/chafin-andrew-40-jersey-youth-nflc">chafin andrew 40 jersey youth</a>
    juliemobley http://www.juliemobley.com/

  12. avatar sguerradesign

    <a href="http://www.adanastarpen.com/kvinners-nike-air-force-1-low-royal-bl%C3%A5-obuvk">kvinners nike air force 1 low royal bl氓</a> <a href="http://www.actconfident.com/air-jordan-horizon-rosa-mint-gr%C3%B8nn-obuvi">air jordan horizon rosa mint gr酶nn</a> <a href="http://www.erzurumihl.com/mujeres-nike-air-max-90-blanco-amarillo-zapatosk">mujeres nike air max 90 blanco amarillo</a> <a href="http://www.candctowingil.com/coach-signature-handbags-clearance-coachh">coach signature handbags clearance</a> <a href="http://www.onlysidekick.com/nike-air-max-express-orange-grey-shoesj">nike air max express orange grey</a> <a href="http://www.rmmassage.com/nike-air-presto-sort-cem%C3%A6ndt-gr%C3%A5-skoq">nike air presto sort cem忙ndt gr氓</a>
    sguerradesign http://www.sguerradesign.com/

  13. avatar webrushllc

    <a href="http://www.goodmedusa.com/clutch-michael-kors-sale-mkh">clutch michael kors sale</a> <a href="http://www.murskacorp.com/adidas-originals-rosa-nubuck-l%C3%A6r-stan-smith-trainers-with-strap-obuvi">adidas originals rosa nubuck l忙r stan smith trainers with strap</a> <a href="http://www.prajansim.com/jordan-1-remasterosso-came-today-2015-scarpek">jordan 1 remasterosso came today 2015</a> <a href="http://www.pokersurga.com/air-jordan-retro-5-low-silver-black-red-shoesi">air jordan retro 5 low silver black red</a> <a href="http://www.arkmmj.com/nike-lab-air-max-2018-armada-azul-blanco-zapatosi">nike lab air max 2018 armada azul blanco</a> <a href="http://www.butterbroad.com/adidas-forum-mid-hvid-bl%C3%A5-skoh">adidas forum mid hvid bl氓</a>
    webrushllc http://www.webrushllc.com/

  14. avatar sidecisions

    <a href="http://www.larrystoverjr.com/adidas-nitrocharge-azul-p%C3%BArpura-chaussuresf">adidas nitrocharge azul p煤rpura</a> <a href="http://www.gokartinfo.com/femminile-nike-air-max-2016-bianca-blu-chaussuresa">femminile nike air max 2016 bianca blu</a> <a href="http://www.enemythunder.com/hombres-nike-flyknit-lunar-3-blanco-naranja-butya">hombres nike flyknit lunar 3 blanco naranja</a> <a href="http://www.lahtacentr.com/adidas-projoator-goalkeeper-gloves-2012-butyc">adidas projoator goalkeeper gloves 2012</a> <a href="http://www.intspv.com/air-jordan-retro-14-kvinders-mintgr%C3%B8n-r%C3%B8d-scarpeg">air jordan retro 14 kvinders mintgr酶n r酶d</a> <a href="http://www.itsmylogo.com/nike-air-force-1-gr%C3%A5-crop-top-chaussuresc">nike air force 1 gr氓 crop top</a>
    [url=http://www.sidecisions.com/]sidecisions[/url]

  15. avatar tobepartridge

    <a href="http://www.tmzred.com/nike-lunar-raid-university-rouge-chaussuresh">nike lunar raid university rouge</a> <a href="http://www.alcovozov.com/prada-bn2076-handbags-in-black-pradah">prada bn2076 handbags in black</a> <a href="http://www.ahmztradefx.com/jason-witten-82-dallas-cowboys-jersey-nfli">jason witten 82 dallas cowboys jersey</a> <a href="http://www.isampled.com/free-og-14-br-gr%C3%B8n-sort-skoq">free og 14 br gr酶n sort</a> <a href="http://www.aldervets.com/polo-ralph-lauren-mens-long-sleeve-polo-poloh">polo ralph lauren mens long sleeve polo</a> <a href="http://www.vanltl.com/asics-onitsuka-tiger-mexico-66-gold-purple-shoesi">asics onitsuka tiger mexico 66 gold purple</a>
    tobepartridge http://www.tobepartridge.com/

  16. avatar 换链神器

    “换链神器” 打造最方便、快捷、安全的换链客户端平台。[#字母#]
    欢迎您的加入!
    http://www.huanlj.com/?t=1421

  17. avatar 铝单板

    写得很详细,一看就懂,谢谢博主分享

  18. avatar 厦门商标

    还是可以对齐使用的

欢迎留言




用户登录

sitemap