• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

2022-02-18 Android LinearLayout布局常用的实现居、左对齐、右对齐

武飞扬头像
海月汐辰
帮助1

一、效果一,居中、左对齐、右对齐。

学新通

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    android:orientation="vertical"
  4.  
    android:id="@ id/activity_main"
  5.  
    android:layout_width="match_parent"
  6.  
    android:layout_height="match_parent"
  7.  
    android:background="@color/purple_200">
  8.  
     
  9.  
     
  10.  
    <TextView
  11.  
    android:id="@ id/textView"
  12.  
    android:layout_width="match_parent"
  13.  
    android:layout_height="wrap_content"
  14.  
    android:text="合计"
  15.  
    android:textSize="20sp"
  16.  
    android:gravity="center"
  17.  
    />
  18.  
     
  19.  
    <TextView
  20.  
    android:id="@ id/textView2"
  21.  
    android:layout_width="match_parent"
  22.  
    android:layout_height="wrap_content"
  23.  
    android:text="108元"
  24.  
    android:textSize="20sp"
  25.  
    android:gravity="center"
  26.  
    android:layout_marginTop="23dp"
  27.  
    />
  28.  
    <!-- 在这个LinearLayout下,不指定orientation就默认horizontal -->
  29.  
    <LinearLayout
  30.  
    android:layout_width="fill_parent"
  31.  
    android:layout_height="wrap_content"
  32.  
    android:layout_marginTop="23dp">
  33.  
     
  34.  
    <TextView
  35.  
    android:layout_width="wrap_content"
  36.  
    android:layout_height="wrap_content"
  37.  
    android:text="总里程:"
  38.  
    />
  39.  
     
  40.  
    <TextView
  41.  
    android:layout_width="match_parent"
  42.  
    android:layout_height="wrap_content"
  43.  
    android:gravity="right"
  44.  
    android:text="266.86公里"
  45.  
    android:id="@ id/textView3"
  46.  
    />
  47.  
    </LinearLayout>
  48.  
     
  49.  
    <LinearLayout
  50.  
    android:layout_width="fill_parent"
  51.  
    android:layout_height="wrap_content"
  52.  
    android:layout_marginTop="23dp">
  53.  
    <TextView
  54.  
    android:layout_width="wrap_content"
  55.  
    android:layout_height="wrap_content"
  56.  
    android:text="运费:"/>
  57.  
    <TextView
  58.  
    android:layout_width="match_parent"
  59.  
    android:layout_height="wrap_content"
  60.  
    android:gravity="right"
  61.  
    android:text="1639元"
  62.  
    />
  63.  
    </LinearLayout>
  64.  
     
  65.  
    <LinearLayout
  66.  
    android:layout_width="fill_parent"
  67.  
    android:layout_height="wrap_content"
  68.  
    android:layout_marginTop="23dp">
  69.  
    <TextView
  70.  
    android:layout_width="wrap_content"
  71.  
    android:layout_height="wrap_content"
  72.  
    android:text="起步价(含5公里):"/>
  73.  
    <TextView
  74.  
    android:layout_width="match_parent"
  75.  
    android:layout_height="wrap_content"
  76.  
    android:gravity="right"
  77.  
    android:text="1571元"
  78.  
    />
  79.  
    </LinearLayout>
  80.  
     
  81.  
    </LinearLayout>
学新通

二、单个控件靠右

学新通

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    xmlns:tools="http://schemas.android.com/tools"
  4.  
    android:orientation="vertical"
  5.  
    android:layout_width="fill_parent"
  6.  
    android:layout_height="fill_parent"
  7.  
    android:background="@color/purple_200"
  8.  
    tools:context=".ArrivedReport">
  9.  
     
  10.  
    <RelativeLayout
  11.  
    android:id="@ id/background"
  12.  
    android:layout_width="fill_parent"
  13.  
    android:layout_height="fill_parent">
  14.  
     
  15.  
    <ImageView
  16.  
    android:id="@ id/backgroundphoto"
  17.  
    android:layout_width="fill_parent"
  18.  
    android:layout_height="fill_parent" />
  19.  
     
  20.  
    <LinearLayout
  21.  
    android:layout_width="fill_parent"
  22.  
    android:layout_height="wrap_content"
  23.  
    android:id="@ id/name_ground"
  24.  
    android:layout_marginTop="93dp"
  25.  
    android:layout_marginLeft="44dp"
  26.  
    android:layout_marginRight="44dp"
  27.  
    android:gravity="right"
  28.  
    android:orientation="horizontal">
  29.  
    <TextView
  30.  
    android:id="@ id/name"
  31.  
    android:layout_width="wrap_content"
  32.  
    android:layout_height="wrap_content"
  33.  
    android:textSize="18dp"
  34.  
    android:text="黄静玲"
  35.  
    android:textColor="@color/black" />
  36.  
    </LinearLayout>
  37.  
     
  38.  
    <LinearLayout
  39.  
    android:orientation="horizontal"
  40.  
    android:layout_width="fill_parent"
  41.  
    android:layout_height="wrap_content"
  42.  
    android:id="@ id/time_ground"
  43.  
    android:layout_below="@ id/name_ground"
  44.  
    android:layout_marginTop="3dp"
  45.  
    android:layout_marginLeft="44dp"
  46.  
    android:layout_marginRight="44dp"
  47.  
    android:gravity="right">
  48.  
    <TextView
  49.  
    android:id="@ id/time"
  50.  
    android:layout_width="wrap_content"
  51.  
    android:layout_height="wrap_content"
  52.  
    android:textSize="18dp"
  53.  
    android:textColor="@color/black"
  54.  
    android:text="2022-02-14 16:58" />
  55.  
    </LinearLayout>
  56.  
    <LinearLayout
  57.  
    android:orientation="horizontal"
  58.  
    android:layout_width="fill_parent"
  59.  
    android:layout_height="wrap_content"
  60.  
    android:layout_below="@ id/time_ground"
  61.  
    android:id="@ id/address_ground"
  62.  
    android:layout_marginTop="44dp"
  63.  
    android:layout_marginLeft="44dp"
  64.  
    android:layout_marginRight="44dp"
  65.  
    android:gravity="center">
  66.  
     
  67.  
    <TextView
  68.  
    android:id="@ id/address"
  69.  
    android:layout_width="wrap_content"
  70.  
    android:layout_height="wrap_content"
  71.  
    android:textSize="23dp"
  72.  
    android:textColor="@color/black"
  73.  
    android:textStyle="bold"
  74.  
    android:text="天涯海角" />
  75.  
    </LinearLayout>
  76.  
     
  77.  
    <TextView
  78.  
    android:id="@ id/test_time"
  79.  
    android:layout_width="wrap_content"
  80.  
    android:layout_height="wrap_content"
  81.  
    android:layout_below="@ id/address_ground"
  82.  
    android:layout_marginLeft="63dp"
  83.  
    android:layout_marginTop="370dp"
  84.  
    android:textSize="14dp"
  85.  
    android:textColor="@color/white"
  86.  
    android:text="2022-02-14 04:41" />
  87.  
    </RelativeLayout>
  88.  
    </LinearLayout>
学新通

三、效果三

学新通

  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="fill_parent"
  5.  
    android:background="@color/purple_200"
  6.  
    android:orientation="horizontal" >
  7.  
     
  8.  
    <TextView
  9.  
    android:layout_width="wrap_content"
  10.  
    android:layout_height="wrap_content"
  11.  
    android:text="左边1" />
  12.  
     
  13.  
    <TextView
  14.  
    android:layout_width="wrap_content"
  15.  
    android:layout_height="wrap_content"
  16.  
    android:text="左边2" />
  17.  
    <!-- 将TextView包在另一个LinearLayout中
  18.  
    注意android:layout_width和android:gravity这两个属性
  19.  
    -->
  20.  
    <LinearLayout
  21.  
    android:layout_width="fill_parent"
  22.  
    android:layout_height="wrap_content"
  23.  
    android:gravity="right" >
  24.  
     
  25.  
    <TextView
  26.  
    android:layout_width="wrap_content"
  27.  
    android:layout_height="wrap_content"
  28.  
    android:layout_marginRight="10dp"
  29.  
    android:text="右边" />
  30.  
    </LinearLayout>
  31.  
     
  32.  
    </LinearLayout>
学新通

四、参考文章

Android布局控件-LinearLayout详解 - 简书

Android学习——LinearLayout布局实现居中、左对齐、右对齐 - 最咸的鱼 - 博客园

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhficgkg
系列文章
更多 icon
同类精品
更多 icon
继续加载