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

2014年07月31日 Android 20条评论 阅读49203次

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
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

20 条评论

  1. <a href="http://www.jotrdesigns.com/asics-gel-lyte-v-for-sale-nj-skof">asics gel lyte v for sale nj</a> <a href="http://www.uilstrategie.com/nike-sb-blazer-low-kvinders-s%C3%B8lv-sportse">nike sb blazer low kvinders s酶lv</a> <a href="http://www.gospeedbird.com/nike-air-max-bw-mens-blue-runningd">nike air max bw mens blue</a> <a href="http://www.blickpoy.com/game-morten-andersen-jersey-new-orleans-saints-7-road-white-nflf">game morten andersen jersey new orleans saints 7 road white</a> <a href="http://www.myserafins.com/nike-hyperdunk-2016-orange-brown-skoa">nike hyperdunk 2016 orange brown</a> <a href="http://www.emmamoney.com/barato-nike-lebron-soldier-9-mujeres-rosado-runningd">barato nike lebron soldier 9 mujeres rosado</a>
    truckent http://www.truckent.com/

  2. <a href="http://www.sethba.com/draymond-green-warriors-jersey-nflr">draymond green warriors jersey</a> <a href="http://www.webgiganten.com/hermes-belt-price-usa-beltr">hermes belt price usa</a> <a href="http://www.gldstore.com/womens-nike-kobe-12-orange-germany-shoeso">womens nike kobe 12 orange germany</a> <a href="http://www.aesqmed.com/jumbo-ugg-boots-uggsr">jumbo ugg boots</a> <a href="http://www.eryasaat.com/nike-hypervenom-neon-yellow-skou">nike hypervenom neon yellow</a> <a href="http://www.jindianhua.com/billig-nike-air-max-1-essential-herren-wei%C3%9F-shoesa">billig nike air max 1 essential herren wei脽</a>
    nonnagrazia http://www.nonnagrazia.com/

  3. <a href="http://www.diplomabroad.com/nike-free-3.0-focfly-so-r%C3%B8d-skok">nike free 3.0 focfly so r酶d</a> <a href="http://www.trechadental.com/nike-air-max-invigor-alle-schwarz-schuheh">nike air max invigor alle schwarz</a> <a href="http://www.akhbarsanaye.com/men-nike-air-max-2017-leather-black-red-2-shoesk">men nike air max 2017 leather black red 2</a> <a href="http://www.timxes.com/nike-hyperdunk-2015-sort-r%C3%B8d-skol">nike hyperdunk 2015 sort r酶d</a> <a href="http://www.leadercheetah.com/air-jordan-retro-12-svart-gamma-bl%C3%A5-obuvk">air jordan retro 12 svart gamma bl氓</a> <a href="http://www.arealleader.com/nike-huarache-ultra-arancia-oro-scarpei">nike huarache ultra arancia oro</a>
    hozloli http://www.hozloli.com/

  4. <a href="http://www.forttr.com/adidas-superstar-red-shoesl">adidas superstar red</a> <a href="http://www.chopkinslaw.com/mens-ralph-lauren-polo-shirt-black-poloh">mens ralph lauren polo shirt black</a> <a href="http://www.plusbolivia.com/nike-mercurial-vapor-x-gr%C3%B8n-skok">nike mercurial vapor x gr酶n</a> <a href="http://www.ahlivv.com/converse-high-blau-schuheh">converse high blau</a> <a href="http://www.bybrettbarner.com/nike-air-presto-palm-green-resort-shoesk">nike air presto palm green resort</a> <a href="http://www.timowallets.com/nike-tanjun-kvinders-bl%C3%A5-skol">nike tanjun kvinders bl氓</a>
    laflammesinc http://www.laflammesinc.com/

  5. <a href="http://www.yeliseeva.com/adidas-forum-mid-violet-argent-chaussuresh">adidas forum mid violet argent</a> <a href="http://www.miembrojr.com/nike-roshe-run-high-grigio-oro-scarpeq">nike roshe run high grigio oro</a> <a href="http://www.faridekhalaf.com/air-jordan-retro-9-womens-yellow-green-shoesj">air jordan retro 9 womens yellow green</a> <a href="http://www.rawbiofast.com/nike-kd-7-kvinders-r%C3%B8d-bl%C3%A5-skoi">nike kd 7 kvinders r酶d bl氓</a> <a href="http://www.mabarrel.com/nike-roshe-one-triple-rosso-scarpel">nike roshe one triple rosso</a> <a href="http://www.almarsecurity.com/womens-nike-lebron-12-grey-black-shoesj">womens nike lebron 12 grey black</a>
    shonema http://www.shonema.com/

  6. <a href="http://www.mikespothole.com/nike-dunk-sort-himmelbl%C3%A5-schuhea">nike dunk sort himmelbl氓</a> <a href="http://www.theclaydoctor.com/adidas-pr%C3%B8dator-lz-trx-fg-for-salg-scarpeb">adidas pr酶dator lz trx fg for salg</a> <a href="http://www.allthatspool.com/womens-nike-huarache-utility-black-denmark-shoeso">womens nike huarache utility black denmark</a> <a href="http://www.ihavewed.com/adidas-projoator-soccer-verde-negro-kurpesb">adidas projoator soccer verde negro</a> <a href="http://www.cloudsmartin.com/new-balance-857-review-zapatost">new balance 857 review</a> <a href="http://www.myachingoa.com/girls-high-converse-shoesz">girls high converse</a>
    divyangmart http://www.divyangmart.com/

  7. <a href="http://www.findatrans.com/air-jordan-retro-6-femmes-oranger-bleu-chaussuresl">air jordan retro 6 femmes oranger bleu</a> <a href="http://www.isabellerigal.com/nike-zoom-pegasus-33-femmes-rose-argent-chaussuresq">nike zoom pegasus 33 femmes rose argent</a> <a href="http://www.esnafkocaeli.com/dam%C3%A4nner-nike-sb-stefan-janoski-max-wei%C3%9F-blau-schuhei">dam盲nner nike sb stefan janoski max wei脽 blau</a> <a href="http://www.dkdjabar.com/dam%C3%A4nner-nike-roshe-run-speckle-rosa-schwarz-schuheh">dam盲nner nike roshe run speckle rosa schwarz</a> <a href="http://www.jamiecirillo.com/kvinners-air-jordan-retro-8-gull-hvit-obuvj">kvinners air jordan retro 8 gull hvit</a> <a href="http://www.xpostshopx.com/nike-kobe-11-grigio-viola-scarpep">nike kobe 11 grigio viola</a>
    cestreaming http://www.cestreaming.com/

  8. <a href="http://www.alpigap.com/nike-kobe-6-naranja-verde-butye">nike kobe 6 naranja verde</a> <a href="http://www.dinerwareslo.com/ralph-lauren-swimwear-caqui-beige-polou">ralph lauren swimwear caqui beige</a> <a href="http://www.eskortpu.com/jordan-retro-12-marine-blau-and-wei%C3%9F-chaussurese">jordan retro 12 marine blau and wei脽</a> <a href="http://www.maribrairie.com/air-max-1-essential-hyper-blu-scarpee">air max 1 essential hyper blu</a> <a href="http://www.myfaccia.com/nfl-rodney-gunter-womens-limited-red-jersey-95-arizona-cardinals-nike-rush-drift-fashion-nfls">nfl rodney gunter womens limited red jersey 95 arizona cardinals nike rush drift fashion</a> <a href="http://www.noteablepost.com/ralph-lauren-hombres-crew-neck-sweaters-polot">ralph lauren hombres crew neck sweaters</a>
    frijlan http://www.frijlan.com/

  9. <a href="http://www.georgesherry.com/asics-in-esecuzione-scarpe-bianca-blu-kurpesc">asics in esecuzione scarpe bianca blu</a> <a href="http://www.jenalgroup.com/ray-ban-rb4098-keyboard-sunglassese">ray ban rb4098 keyboard</a> <a href="http://www.badacamshotel.com/limited-nike-white-mens-ibraheim-campbell-alternate-jersey-nfl-20-dallas-cowboys-throwback-nflr">limited nike white mens ibraheim campbell alternate jersey nfl 20 dallas cowboys throwback</a> <a href="http://www.compressmyjs.com/air-max-90-ultra-2.0-flyknit-black-skoa">air max 90 ultra 2.0 flyknit black</a> <a href="http://www.fetison.com/jordan-retro-4-powder-blue-2015-sneakerf">jordan retro 4 powder blue 2015</a> <a href="http://www.neffloot.com/asics-gel-noosa-tri-9-dam%C3%A4nner-laufen-schuhe-uk-online-kurpesa">asics gel noosa tri 9 dam盲nner laufen schuhe uk online</a>
    canditatenext http://www.canditatenext.com/

  10. avatar milligande

    <a href="http://www.waweventos.com/vibram-fivefingers-sprint-hvid-bl%C3%A5-obuve">vibram fivefingers sprint hvid bl氓</a> <a href="http://www.alarmselpaso.com/nike-zoom-vomero-hommes-fonctionnement-chaussures-girl-kurpesf">nike zoom vomero hommes fonctionnement chaussures girl</a> <a href="http://www.ceritaboruto.com/nike-lunarelite-2-femmes-fonctionnement-chaussure-obuvb">nike lunarelite 2 femmes fonctionnement chaussure</a> <a href="http://www.demngoibet.com/jordan-melo-m10-wei%C3%9F-orange-butya">jordan melo m10 wei脽 orange</a> <a href="http://www.redchipit.com/game-womens-aaron-colvin-navy-blue-home-jersey-nfl-nike-houston-texans-22-nflu">game womens aaron colvin navy blue home jersey nfl nike houston texans 22</a> <a href="http://www.introsphera.com/nike-roshe-run-floral-ni%C3%B1os-amarillo-blanco-kurpesb">nike roshe run floral ni帽os amarillo blanco</a>
    milligande http://www.milligande.com/

  11. avatar phxfam

    <a href="http://www.kainjipark.com/argentina-2-mammana-home-soccer-country-jersey-nflw">argentina 2 mammana home soccer country jersey</a> <a href="http://www.lapthelakes.com/chicago-bulls-michael-jordan-hoodie-new-adult-size-medium-other-nflv">chicago bulls michael jordan hoodie new adult size medium other</a> <a href="http://www.delmaroffice.com/nike-tiempo-legend-v-sg-pro-skog">nike tiempo legend v sg pro</a> <a href="http://www.medieneinsatz.com/nike-free-5.0-v2-dam%C3%A4nner-skob">nike free 5.0 v2 dam盲nner</a> <a href="http://www.linexcanton.com/pittsburgh-pirates-flat-top-hat-zone-number-hata">pittsburgh pirates flat top hat zone number</a> <a href="http://www.saskostomy.com/nike-air-jordan-1-fragment-uk-runningf">nike air jordan 1 fragment uk</a>
    phxfam http://www.phxfam.com/

  12. avatar izpanno

    <a href="http://www.redstonecloud.com/nike-femmes-lunarglide-6-chaussures-butya">nike femmes lunarglide 6 chaussures</a> <a href="http://www.thepoolfloats.com/nfl-womens-nike-buffalo-bills-11-zay-jones-ash-backer-pullover-hoodie-nflu">nfl womens nike buffalo bills 11 zay jones ash backer pullover hoodie</a> <a href="http://www.tsgctt.com/asics-gel-kayano-21-nero-lightning-vape-kurpesb">asics gel kayano 21 nero lightning vape</a> <a href="http://www.virginiaflyin.com/adidas-superstar-mujeres-venta-qld-schuhec">adidas superstar mujeres venta qld</a> <a href="http://www.bandbwhitby.com/nike-kobe-11-tutti-nero-names-schuheg">nike kobe 11 tutti nero names</a> <a href="http://www.funkymediamx.com/kvinders-nike-pale-bl%C3%A5-blazer-mid-trainers-kurpesc">kvinders nike pale bl氓 blazer mid trainers</a>
    izpanno http://www.izpanno.com/

  13. avatar noumetpatrice

    <a href="http://www.dazzlle.com/nike-air-max-invigor-mens-gold-red-skob">nike air max invigor mens gold red</a> <a href="http://www.fitwizhawaii.com/burberry-light-scarf-scarfr">burberry light scarf</a> <a href="http://www.lojandreia.com/nike-kwazi-action-kvinders-bl%C3%A5-sneakerf">nike kwazi action kvinders bl氓</a> <a href="http://www.robinliz.com/billig-nike-free-nr-dam%C3%A4nner-gold-shoesa">billig nike free nr dam盲nner gold</a> <a href="http://www.tracyrn.com/oakley-deviation-team-usa-sunglasses-sunglassesr">oakley deviation team usa sunglasses</a> <a href="http://www.primehostinc.com/ralph-lauren-polo-big-pony-aftershave-qatar-polob">ralph lauren polo big pony aftershave qatar</a>
    noumetpatrice http://www.noumetpatrice.com/

  14. avatar cletalover

    <a href="http://www.ehomeoclinic.com/nike-air-max-2017-flyknit-rosa-blu-butye">nike air max 2017 flyknit rosa blu</a> <a href="http://www.geuwompo.com/ralph-lauren-ocean-challenge-polo-shirt-norway-polou">ralph lauren ocean challenge polo shirt norway</a> <a href="http://www.inckua.com/nike-zoom-vomero-kvinners-bl%C3%A5-svart-chaussurese">nike zoom vomero kvinners bl氓 svart</a> <a href="http://www.infantocursos.com/adidas-zx-750-herre-sko-online-scarpee">adidas zx 750 herre sko online</a> <a href="http://www.omegaspark.com/nike-miami-dolphins-womens-legend-logo-drifit-nfl-t-shirt-white-nfls">nike miami dolphins womens legend logo drifit nfl t shirt white</a> <a href="http://www.radsportland.com/mennns-ralph-lauren-jackets-rosa-lilla-polot">mennns ralph lauren jackets rosa lilla</a>
    cletalover http://www.cletalover.com/

  15. avatar garajebeobide

    <a href="http://www.masterelief.com/nike-kobe-vii-wolf-grigio-kurpese">nike kobe vii wolf grigio</a> <a href="http://www.nwlcenter.com/limited-mens-martinas-rankin-red-alternate-jersey-nfl-nike-houston-texans-75-vapor-untouchable-nflr">limited mens martinas rankin red alternate jersey nfl nike houston texans 75 vapor untouchable</a> <a href="http://www.piezoinjector.com/nike-air-flight-89-fusion-rose-keychain-kurpese">nike air flight 89 fusion rose keychain</a> <a href="http://www.printonlipro.com/herren-air-max-90-em-blau-wei%C3%9F-schuhee">herren air max 90 em blau wei脽</a> <a href="http://www.biagricola.com/nike-air-huarache-svart-elephant-obuvg">nike air huarache svart elephant</a> <a href="http://www.dennygorman.com/hombres-nike-free-3.0-v5-blanco-negro-chaussuresa">hombres nike free 3.0 v5 blanco negro</a>
    garajebeobide http://www.garajebeobide.com/

  16. avatar frugstore

    <a href="http://www.tdsurvivalkit.com/adidas-originals-zx-flux-zapatilla-de-deporte-negro-bahia-mint-butya">adidas originals zx flux zapatilla de deporte negro bahia mint</a> <a href="http://www.zpackrat.com/femmes-nike-kd-8-vert-bleu-chaussurese">femmes nike kd 8 vert bleu</a> <a href="http://www.antarlor.com/nike-lebron-soldier-9-nero-and-rosso-butya">nike lebron soldier 9 nero and rosso</a> <a href="http://www.asynea.com/coles-air-jordan-3-retro-db-hommes-chaussure-schuheg">coles air jordan 3 retro db hommes chaussure</a> <a href="http://www.barinotomotiv.com/ralph-lauren-coats-lilla-hvid-poloz">ralph lauren coats lilla hvid</a> <a href="http://www.cakemilitia.com/ni%C3%B1os-zapatos-rosado-blanco-chaussuresa">ni帽os zapatos rosado blanco</a>
    [url=http://www.frugstore.com/]frugstore[/url]

  17. avatar kguides

    <a href="http://www.ictbel.com/adidas-prossoator-beast-pack-schuhea">adidas prossoator beast pack</a> <a href="http://www.ioioland.com/nike-air-presto-anti-fur-vin-hvit-scarpeb">nike air presto anti fur vin hvit</a> <a href="http://www.sesliesenyurt.com/nike-air-max-95-green-spain-shoeso">nike air max 95 green spain</a> <a href="http://www.wethinkjerky.com/adidas-ultra-boost-mennns-gr%C3%A5-kurpesb">adidas ultra boost mennns gr氓</a> <a href="http://www.dadees.com/adidas-sneakers-price-zapatost">adidas sneakers price</a> <a href="http://www.pickwickcrc.com/custom-painted-converse-uk-shoesz">custom painted converse uk</a>
    kguides http://www.kguides.com/

  18. avatar 换链神器

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

  19. avatar 铝单板

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

  20. avatar 厦门商标

    还是可以对齐使用的

欢迎留言




用户登录

sitemap